{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# file header"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys,os,inspect\n",
    "if hasattr(sys.modules[__name__], '__file__'):\n",
    "    _file_name = __file__\n",
    "else:\n",
    "    _file_name = inspect.getfile(inspect.currentframe())\n",
    "CURRENT_FILE_PATH = os.path.dirname(_file_name)\n",
    "sys.path.append(os.getcwd()+\"/../../neuronVis\")\n",
    "from cx_tool import *\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# data information"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_path = '../../resource/project_cortex_to_brainstem_data_v4/'\n",
    "create_dir(data_path)\n",
    "cortex_region = ['FRP','PL','ILA','ORB','ACA','AI','GU','VISC','PTLp','RSP','TEa','ECT','PERI','MOs','MOp','SSp','SSs','AUD','VIS']\n",
    "# to facilidate using, also adding extra information\n",
    "brainstem_region_hierachy = {\n",
    "    'Cortex':['FRP','PL','ILA','ORB','ACA','AI','GU','VISC','PTLp','RSP','TEa','ECT','PERI','MOs','MOp','SSp','SSs','AUD','VIS'],\n",
    "    'OLF':['MOB','AOB','AON','TT','DP','PIR','NLOT','COA','PAA','TR'],\n",
    "    'HPF':['CA','DG','FC','IG','ENT','PAR','POST','PRE','SUB','ProS','HATA','APr'],\n",
    "    'Cortex-sp':['CLA','EP','LA','BLA','BMA','PA'],\n",
    "    'CNU':['STRd','ACB','FS','OT','LSX','AAA','CEA','MEA','PALd','PALv','PALm','PALc'],\n",
    "    'TH':['VAL','VM','VPL','VPM','VPLpc','VPMpc','PoT','SPA','SPFm','SPFp','PP','MG','LGd','LP','PO','POL','SGN','Eth','AV','AM','AD','IAM','IAD','LD',\n",
    "    'IMD','MD','SMT','PR','PVT','PT','RE','Xi','RH','CM','PCN','CL','PF','PIL','GENv','EPI','RT'],\n",
    "    'HY':['SO','ASO','PVH','PVa','PVi','ARH','ADP','AVP','AVPV','DMH','MEPO','MPO','OV','PD','PS','PVp','PVpo','SBPV','SCH','SFO','VMPO','VLPO','AHN',\n",
    "    'MBO','MPN','PMd','PMv','PVHd','VMH','PH','LHA','LPO','PST','PSTN','PeF','RCH','STN','TU','ZI','ME'],\n",
    "    'MB':['APN','NOT','NPC','PPT','RPF','AT','MPT','OP','VTA','SNr','SNc','SCs','SCm','NB','SAG','PBG','IC','RR','MRN','RN',\n",
    "    'PAG','PRC','INC','ND','Su3','CUN','PPN','IF','IPN','RL','CLI','DR','III','MA3','EW','VTN'],\n",
    "    'P':['PSV','PB','PCG','PG','PRNr','PRNc','SUT','TRN','V','P5','CS','LDT','NI','RPO','SOC','NLL','DTN','PDTg','SG','I5','SLC','SLD'],\n",
    "    'MY':['DCO','VCO','LAV','MV','SPIV','SUV','DMX','GR','CU','ECU','NTS','NTB','SPVC','SPVI','SPVO','Pa5','VII','GRN','IRN','LIN','LRNm','LRNp','MARN',\n",
    "    'MDRNd','MDRNv','PARN','PGRNd','PGRNl','PHY','VI','AMB','ICB','PAS','PRP','PPY','x','XII','y','IO','RM','RPA','RO'],\n",
    "    'CB':['VERM','HEM','FN','IP','DN','VeCB'],\n",
    "    'FT':['py','pyd'],\n",
    "    'SC':['unknow']\n",
    "}\n",
    "cortex_region_to_subregion,cortex_subregion_to_region = get_subregions(cortex_region)\n",
    "brainstem_region = []\n",
    "for a in brainstem_region_hierachy:\n",
    "    for b in brainstem_region_hierachy[a]:\n",
    "        brainstem_region.append(b)\n",
    "brainstem_region_to_subregion,brainstem_subregion_to_region = get_subregions(brainstem_region)\n",
    "cortex_neuron_name = []\n",
    "cortical_module = {'SS':['SSp','SSs'],'MO':['MOp','MOs'],'PFC':['ILA','PL','FRP','ORB'],'Insula':['GU','VISC','AI'],'Cingulate':['ACA','RSP'],'VIS':['VIS','PTLp'],'AUD':['AUD'],'TEa':['TEa','ECT','PERI']}\n",
    "cortical_module_color = {'SS':color_pool[1],'MO':color_pool[0],'VIS':color_pool[3],'AUD':color_pool[2],'Cingulate':color_pool[4],'Insula':color_pool[5],'PFC':color_pool[7],'TEa':color_pool[6]}\n",
    "cortical_module_transform = {}\n",
    "for i in cortical_module:\n",
    "    for j in cortical_module[i]:\n",
    "        cortical_module_transform[j] = i\n",
    "_flatmap = cv.imread('../../resource/flatmapedge2.tif',1)\n",
    "_flatmap_mask = cv.resize(cv.imread('../../resource/flatmap.png',1),(_flatmap.shape[0],_flatmap.shape[1]))\n",
    "_flatmap_name = cv.resize(cv.imread('../../resource/flatmapedge.png',1),(_flatmap.shape[0],_flatmap.shape[1]))\n",
    "flatmap_mask = np.zeros((_flatmap.shape[0],_flatmap.shape[1],4))\n",
    "flatmap_mask[...,:3] = (_flatmap_mask==0)*255\n",
    "flatmap_mask[...,3] = np.where(flatmap_mask[...,0]==0,0,1)\n",
    "flatmap = np.zeros((_flatmap.shape[0],_flatmap.shape[1],4))\n",
    "flatmap[...,-1] = np.where(_flatmap_mask[...,0]==0,1.0,flatmap[...,-1])\n",
    "flatmap[...,0] = 255-(np.mean(_flatmap_name,axis=-1)<200)*255\n",
    "flatmap[...,1] = 255-(np.mean(_flatmap_name,axis=-1)<200)*255\n",
    "flatmap[...,2] = 255-(np.mean(_flatmap_name,axis=-1)<200)*255\n",
    "flatmap[...,3] = np.mean(_flatmap_name,axis=-1)<200\n",
    "flatmap2 = np.zeros((_flatmap.shape[0],_flatmap.shape[1],4))\n",
    "flatmap2[...,-1] = np.where(_flatmap_mask[...,0]==0,1.0,flatmap[...,-1])\n",
    "flatmap2[...,0] = 255-(np.mean(_flatmap,axis=-1)<200)*255\n",
    "flatmap2[...,1] = 255-(np.mean(_flatmap,axis=-1)<200)*255\n",
    "flatmap2[...,2] = 255-(np.mean(_flatmap,axis=-1)<200)*255\n",
    "flatmap2[...,3] = np.mean(_flatmap,axis=-1)<200\n",
    "a,b = get_value_range(1-flatmap[...,0]/255)\n",
    "tmp = [np.mean(a),np.mean(b)]\n",
    "tmp2 = np.max([a[1]-a[0],b[1]-b[0]])\n",
    "flatten_map_r = [tmp[0]-tmp2/2,tmp[0]+tmp2/2,tmp[1]+tmp2/2,tmp[1]-tmp2/2]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## PT neuron selection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_neuron_list_from_project(name):\n",
    "    tmp = []\n",
    "    for i in iondata.getSampleInfo(projectID=name):\n",
    "        for j in iondata.getNeuronListBySampleID(str(i['fMOST_id'])):\n",
    "            tmp.append(j['sampleid']+j['name'])\n",
    "    return tmp\n",
    "if not os.path.exists(data_path+'PT.nv'):\n",
    "    cortex_neuron_name = []\n",
    "    tmp = get_neuron_list_from_project('ISO')\n",
    "    # since unknow contains spine, but it will include a lot of IT and CT\n",
    "    PT_check_region_list = ['MB','P','MY','unknow']\n",
    "    for i in tool_bar(tmp,desc='generating PT neuron list...',leave=False):\n",
    "        control_console_output(0)\n",
    "        region = iondata.getNeuronPropertyByID(i[:-7],i[-7:])['somaregion']\n",
    "        if np.sum(get_axonal_statics_by_regions(i,PT_check_region_list,'projectregion'))>0 and region in cortex_subregion_to_region:\n",
    "            cortex_neuron_name.append(i)\n",
    "        control_console_output(1)\n",
    "    save_scene(cortex_neuron_name,[1,0,0],data_path+'PT')\n",
    "    # to do check manually\n",
    "    if 1:\n",
    "        step = 500\n",
    "        for i in range(int(np.ceil(float(len(cortex_neuron_name))/step))):\n",
    "            save_scene(cortex_neuron_name[i*step:(i+1)*step],[1,0,0],data_path+'PT_'+str(i))\n",
    "else:\n",
    "    cortex_neuron_name = []\n",
    "    for i in Scene.scene2List(data_path+'PT.nv'):\n",
    "        cortex_neuron_name.append(i['sampleid']+i['name'])\n",
    "print('currently there are ',len(cortex_neuron_name),' neurons in total.')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## information"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sample_info = {}\n",
    "for neuron in cortex_neuron_name:\n",
    "    if neuron[:-7] not in sample_info:\n",
    "        sample_info[neuron[:-7]] =iondata.getSampleInfo(neuron[:-7])[0]\n",
    "f = open(data_path+'sample_info.txt',mode='x')\n",
    "f.write('@'.join(['mouse_sample']+list(sample_info['212064'].keys())).replace('\\n',' ')+'\\n')\n",
    "for i in sample_info:\n",
    "    f.write('@'.join(np.array([i]+list(sample_info[i].values())).astype('str').tolist()).replace('\\n',' ')+'\\n')\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if not os.path.exists(data_path+'sample_info.npy'):\n",
    "    sample_info = {}\n",
    "    for neuron in cortex_neuron_name:\n",
    "        if neuron[:-7] not in sample_info:\n",
    "            sample_info[neuron[:-7]] = {'neurons':[],'gene':iondata.getSampleInfo(neuron[:-7])[0]['transgenic_line']}\n",
    "        sample_info[neuron[:-7]]['neurons'].append(neuron)\n",
    "    np.save(data_path+'sample_info.npy',sample_info)\n",
    "    print('Sample info generated.')\n",
    "else:\n",
    "    sample_info = np.load(data_path+'sample_info.npy',allow_pickle=True).item()\n",
    "    print('Sample info is loaded.')\n",
    "if not os.path.exists(data_path+'neuron_to_brainstem_information.npy'):\n",
    "    neuron_to_brainstem_information = {}\n",
    "    for i in tool_bar(cortex_neuron_name,desc='generating neuron information...',leave=False):\n",
    "        neuron_to_brainstem_information[i] = {}\n",
    "        neuron_property = iondata.getNeuronPropertyByID(i[:-7],i[-7:])\n",
    "        neuron_to_brainstem_information[i]['soma_position'] = neuron_property['somapoint']\n",
    "        neuron_to_brainstem_information[i]['soma_hemisphere'] = 'left' if neuron_property['somapoint'][2]<5700 else 'right'\n",
    "        a,b = region_layer_seperate(neuron_property['somaregion'])\n",
    "        neuron_to_brainstem_information[i]['cortex_region'] = cortex_subregion_to_region[neuron_property['somaregion']]\n",
    "        neuron_to_brainstem_information[i]['cortex_subregion'] = a[len(neuron_to_brainstem_information[i]['cortex_region']):]\n",
    "        if neuron_to_brainstem_information[i]['cortex_region']=='PTLp' and a[:4]!='PTLp':\n",
    "            neuron_to_brainstem_information[i]['cortex_subregion'] = a[3:]\n",
    "        neuron_to_brainstem_information[i]['cortex_layer'] = b\n",
    "        if len(neuron_to_brainstem_information[i]['cortex_subregion'])!=0 and neuron_to_brainstem_information[i]['cortex_subregion']=='-':\n",
    "            neuron_to_brainstem_information[i]['cortex_subregion'] = neuron_to_brainstem_information[i]['cortex_subregion'][1:]\n",
    "        neuron_to_brainstem_information[i]['axonal_projection_length_combined'] = get_axonal_statics_by_regions(i,brainstem_region,'projectregion')\n",
    "        neuron_to_brainstem_information[i]['axonal_projection_length_left'] = get_axonal_statics_by_regions(i,brainstem_region,'projectleftregion')\n",
    "        neuron_to_brainstem_information[i]['axonal_projection_length_right'] = get_axonal_statics_by_regions(i,brainstem_region,'projectrightregion')\n",
    "        neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'] = get_axonal_statics_by_regions(i,brainstem_region,'terminalregion')\n",
    "        neuron_to_brainstem_information[i]['axonal_projection_terminal_left'] = get_axonal_statics_by_regions(i,brainstem_region,'terminalleftregion')\n",
    "        neuron_to_brainstem_information[i]['axonal_projection_terminal_right'] = get_axonal_statics_by_regions(i,brainstem_region,'terminalrightregion')\n",
    "        neuron_to_brainstem_information[i]['dendrite_terminal_point'] = []\n",
    "        neuron_to_brainstem_information[i]['axon_terminal_point'] = []\n",
    "        control_console_output(0)\n",
    "        neuron_tree = iondata.getNeuronTreeByID(i[:-7],i[-7:])\n",
    "        control_console_output(1)\n",
    "        for terminal in neuron_tree.terminals:\n",
    "            if terminal.type==3:\n",
    "                neuron_to_brainstem_information[i]['dendrite_terminal_point'].append(terminal.xyz)\n",
    "            else:\n",
    "                neuron_to_brainstem_information[i]['axon_terminal_point'].append(terminal.xyz)\n",
    "    tmp = []\n",
    "    for i in cortex_neuron_name:\n",
    "        tmp.append(get_mirrored_point(neuron_to_brainstem_information[i]['soma_position']))\n",
    "    for i,j in zip(cortex_neuron_name,compute_flatten_position_and_depth(np.array(tmp))):\n",
    "        neuron_to_brainstem_information[i]['soma_depth'] = j[0]\n",
    "        neuron_to_brainstem_information[i]['soma_outter_position'] = j[1]\n",
    "        neuron_to_brainstem_information[i]['soma_inner_position'] = j[2]\n",
    "        neuron_to_brainstem_information[i]['soma_flatten_position'] = j[3]\n",
    "    density_info = {}\n",
    "    r = 250\n",
    "    X = []\n",
    "    for neuron in neuron_to_brainstem_information:\n",
    "        if len(neuron_to_brainstem_information[neuron]['soma_flatten_position'])>0:\n",
    "            X.append(neuron_to_brainstem_information[neuron]['soma_flatten_position'])\n",
    "    X = np.array(X)\n",
    "    density_info = {}\n",
    "    for neuron in neuron_to_brainstem_information:\n",
    "        if len(neuron_to_brainstem_information[neuron]['soma_flatten_position'])>0:\n",
    "            tmp = np.linalg.norm(np.array(neuron_to_brainstem_information[neuron]['soma_flatten_position'])-X,axis=-1)\n",
    "            mask = np.array(tmp<r)\n",
    "            density_info[neuron] = np.sum(mask*(r-tmp))\n",
    "    _max = np.max(list(density_info.values()))\n",
    "    for neuron in neuron_to_brainstem_information:\n",
    "        if len(neuron_to_brainstem_information[neuron]['soma_flatten_position'])>0:\n",
    "            neuron_to_brainstem_information[neuron]['relative_density'] = density_info[neuron]/_max\n",
    "    np.save(data_path+'neuron_to_brainstem_information.npy',neuron_to_brainstem_information)\n",
    "    print('all information of ',len(cortex_neuron_name),' neurons are generated.')\n",
    "else:\n",
    "    neuron_to_brainstem_information = np.load(data_path+'neuron_to_brainstem_information.npy',allow_pickle=True).item()\n",
    "    print('The data of ',len(cortex_neuron_name),' neurons have been read.')\n",
    "if not os.path.exists(data_path+'neuron_terminals.npy'):\n",
    "    neuron_terminals = {}\n",
    "    for neuron in tool_bar(cortex_neuron_name,desc='generating neuron information...',leave=False):\n",
    "        control_console_output(0)\n",
    "        neuron_tree = get_neuron_tree(neuron)\n",
    "        control_console_output(1)\n",
    "        neuron_terminals[neuron] = {'mirror_mark':neuron_tree.root.z>5700,'terminals':[]}\n",
    "        for terminal in neuron_tree.terminals:\n",
    "            neuron_terminals[neuron]['terminals'].append(terminal.xyz)\n",
    "        neuron_to_brainstem_information[neuron]['axonal_projection_terminal_combined'][brainstem_region.index('unknow')] = np.sum(np.array(neuron_terminals[neuron]['terminals'])[:,0]>13200)\n",
    "        a = np.array(neuron_terminals[neuron]['terminals'])[:,0]>13200\n",
    "        b = np.array(neuron_terminals[neuron]['terminals'])[:,2]<5700\n",
    "        neuron_to_brainstem_information[neuron]['axonal_projection_terminal_left'][brainstem_region.index('unknow')] = np.sum(a*b)\n",
    "        neuron_to_brainstem_information[neuron]['axonal_projection_terminal_right'][brainstem_region.index('unknow')] = np.sum(a^a*b)\n",
    "        if neuron_to_brainstem_information[neuron]['soma_hemisphere']=='right':\n",
    "            neuron_to_brainstem_information[neuron]['axonal_projection_terminal_left'][brainstem_region.index('unknow')] = np.sum(a^a*b)\n",
    "            neuron_to_brainstem_information[neuron]['axonal_projection_terminal_right'][brainstem_region.index('unknow')] = np.sum(a*b)\n",
    "    np.save(data_path+'neuron_to_brainstem_information.npy',neuron_to_brainstem_information)\n",
    "    np.save(data_path+'neuron_terminals.npy',neuron_terminals)\n",
    "else:\n",
    "    neuron_terminals = np.load(data_path+'neuron_terminals.npy',allow_pickle=True).item()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## auxilary functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_neuron_axon_info(neurons,terminal_or_length='length',if_combined = False,if_log = True,filter=[]):\n",
    "    region_name = brainstem_region\n",
    "    if if_combined:\n",
    "        cortex_neuron_axonal_length_by_region = []\n",
    "        for i in neurons:\n",
    "            cortex_neuron_axonal_length_by_region.append([neuron_to_brainstem_information[i]['axonal_projection_'+terminal_or_length+'_combined']])\n",
    "        cortex_neuron_axonal_length_by_region = np.array(cortex_neuron_axonal_length_by_region).reshape(-1,len(brainstem_region))\n",
    "        for i in brainstem_region_hierachy['FT']:\n",
    "            cortex_neuron_axonal_length_by_region[:,region_name.index(i)] *= 1\n",
    "        for i in brainstem_region_hierachy['SC']:\n",
    "            cortex_neuron_axonal_length_by_region[:,region_name.index(i)] *= 1\n",
    "    else:\n",
    "        cortex_neuron_axonal_length_by_region = []\n",
    "        for i in neurons:\n",
    "            tmp_left = neuron_to_brainstem_information[i]['axonal_projection_'+terminal_or_length+'_left']\n",
    "            tmp_right = neuron_to_brainstem_information[i]['axonal_projection_'+terminal_or_length+'_right']\n",
    "            if neuron_to_brainstem_information[i]['soma_hemisphere']=='left':\n",
    "                cortex_neuron_axonal_length_by_region.append([tmp_left,tmp_right])\n",
    "            else:\n",
    "                cortex_neuron_axonal_length_by_region.append([tmp_right,tmp_left])\n",
    "        cortex_neuron_axonal_length_by_region = np.array(cortex_neuron_axonal_length_by_region).reshape(-1,2*len(region_name))\n",
    "        for i in brainstem_region_hierachy['FT']:\n",
    "            cortex_neuron_axonal_length_by_region[:,region_name.index(i)] *= 1\n",
    "            cortex_neuron_axonal_length_by_region[:,region_name.index(i)+len(region_name)] *= 1\n",
    "        for i in brainstem_region_hierachy['SC']:\n",
    "            cortex_neuron_axonal_length_by_region[:,region_name.index(i)] += cortex_neuron_axonal_length_by_region[:,region_name.index(i)+len(region_name)]\n",
    "            cortex_neuron_axonal_length_by_region[:,region_name.index(i)] *= 1\n",
    "            cortex_neuron_axonal_length_by_region[:,region_name.index(i)+len(region_name)] = 0\n",
    "    if if_log:\n",
    "        cortex_neuron_axonal_length_by_region = np.log(cortex_neuron_axonal_length_by_region+1)\n",
    "    if len(filter)!=0:\n",
    "        tmp = [brainstem_region.index(i) for i in filter]\n",
    "        region_name = [brainstem_region[i] for i in tmp]\n",
    "        if not if_combined:\n",
    "            tmp += [brainstem_region.index(i)+len(brainstem_region) for i in filter]\n",
    "        cortex_neuron_axonal_length_by_region = np.array([cortex_neuron_axonal_length_by_region[:,i] for i in tmp]).T\n",
    "    return [region_name,cortex_neuron_axonal_length_by_region]\n",
    "def cluster_neuron(neurons,regions,feature,threhold,save_path,regeneration=False,circle_size=15):\n",
    "    fig = plt.figure(figsize=(10,5))\n",
    "    cortex_cluster_object = hierarchy.dendrogram(hierarchy.linkage(feature,'ward'),color_threshold=threhold,distance_sort=True)\n",
    "    plt.axhline(threhold, linestyle='dashed', color='#AAAAAA')\n",
    "    plt.title(\"Hierachy of the clustering\",fontsize=fontsize_title)\n",
    "    plt.locator_params(axis='y', nbins=3)\n",
    "    plt.xticks([],[])\n",
    "    plt.xlabel('neurons = '+str(len(neurons)),fontsize=fontsize_label)\n",
    "    plt.ylabel('Cluster distance',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "    plt.savefig(save_path+'cluster_hierachy.pdf',format='pdf')\n",
    "    plt.show()\n",
    "        \n",
    "    cluster_object_parse = [[],[]]\n",
    "    neuron_to_cluster = {}\n",
    "    for i,j in zip(cortex_cluster_object['leaves'],cortex_cluster_object['leaves_color_list']):\n",
    "        neuron = neurons[i]\n",
    "        projection_matrix = feature[i]\n",
    "        cluster_object_parse[0].append(color_pool_hex.index(j))\n",
    "        cluster_object_parse[1].append(projection_matrix)\n",
    "        neuron_to_cluster[neuron] = color_pool_hex.index(j)\n",
    "    cluster_object_parse[1] = np.array(cluster_object_parse[1])\n",
    "    cluster_object_parse_visualization = np.zeros(shape=(len(neurons),1+len(regions),3))\n",
    "    cluster_object_parse_visualization[:,0,:] = np.array([color_pool[i] for i in cluster_object_parse[0]])\n",
    "    tmp = np.array([cluster_object_parse[1][:,:len(regions)],cluster_object_parse[1][:,:len(regions)]*0,cluster_object_parse[1][:,len(regions):]]).transpose((1,2,0))\n",
    "    tmp /= np.max(tmp)\n",
    "    cluster_object_parse_visualization[:,1:,:] = tmp*1\n",
    "    fig = plt.figure(figsize=(4*2*2.25,12*2))\n",
    "    ax = fig.add_subplot()\n",
    "    ax.matshow(cluster_object_parse_visualization.transpose((1,0,2)))\n",
    "    _ = ax.set_xticks([],[])\n",
    "    ax.set_xlabel('neurons = '+str(len(neurons)),fontsize=fontsize_label)\n",
    "    cluster_object_parse_visualization_yticks = [[0],['class']]\n",
    "    cluster_object_parse_visualization_yticks[0] += [1+i for i in range(len(regions))]\n",
    "    cluster_object_parse_visualization_yticks[1] += [i if i!='unknow' else 'Spine' for i in regions]\n",
    "    _ = ax.set_yticks(cluster_object_parse_visualization_yticks[0],cluster_object_parse_visualization_yticks[1],fontsize=fontsize_ticks/2)\n",
    "    plt.savefig(save_path+'cluster_projection_matrix.pdf',format='pdf')\n",
    "    plt.show()\n",
    "    cluster_info = {}\n",
    "    for i,j in enumerate(neurons):\n",
    "        _cluster = color_pool_hex.index(cortex_cluster_object['leaves_color_list'][cortex_cluster_object['leaves'].index(i)])\n",
    "        if _cluster not in cluster_info:\n",
    "            cluster_info[_cluster] = []\n",
    "        cluster_info[_cluster].append([j,neuron_to_brainstem_information[j]['soma_flatten_position']])\n",
    "    flatmap = _flatmap*1\n",
    "    for i in range(len(list(cluster_info.keys()))):\n",
    "        tmp3 = []\n",
    "        for j in cluster_info[i]:\n",
    "            if np.sum(abs(np.array(j[1])))!=0:\n",
    "                cv.circle(flatmap,[int(j[1][0])*2, int(j[1][1])*2],circle_size,color_pool[i]*255,-1)\n",
    "            tmp3.append(j[0])\n",
    "        save_scene(tmp3,color_pool[i],save_path+'cluster_'+str(i+1))\n",
    "    fig = plt.figure(figsize=(10,10))\n",
    "    plt.imshow(flatmap)\n",
    "    plt.xlim(300,2700)\n",
    "    plt.ylim(2700,300)\n",
    "    plt.title(\"The soma distribution of the clusters\",fontsize=fontsize_title)\n",
    "    plt.axis('off')\n",
    "    plt.savefig(save_path+'cluster_soma_distribution.pdf',format='pdf')\n",
    "    plt.show()\n",
    "    np.save(save_path+'cluster_info_dict.npy',cluster_info)\n",
    "    cluster_info_transform = {}\n",
    "    for i in cluster_info:\n",
    "        for j in cluster_info[i]:\n",
    "            cluster_info_transform[j[0]] = i\n",
    "    np.save(save_path+'cluster_info_transform_dict.npy',cluster_info_transform)\n",
    "    return cluster_info_transform\n",
    "def plot_projection_density(neuron_list,region_list,sampling_weight=None,th=0.0,save_path='./',name='tmp',cmap='Reds',if_mixed_gaussian=False):\n",
    "    X = []\n",
    "    Y = []\n",
    "    Z = []\n",
    "    for i in neuron_list:\n",
    "        tmp = []\n",
    "        for j in region_list:\n",
    "            try:\n",
    "                tmp.append(neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][brainstem_region.index(j)])\n",
    "            except:\n",
    "                tmp.append(get_axonal_statics_by_regions(i,[j],'terminalregion')[0])\n",
    "        if np.max(tmp)<th:\n",
    "            continue\n",
    "        if if_mixed_gaussian:\n",
    "            X.append(tmp)\n",
    "        else:\n",
    "            X.append(np.sum(tmp))\n",
    "        Y.append(neuron_to_brainstem_information[i]['soma_flatten_position'])\n",
    "        Z.append(i)\n",
    "    Y = np.array(Y)\n",
    "    if if_mixed_gaussian:\n",
    "        X = np.array(X).astype(float)\n",
    "        for i in range(X.shape[1]):\n",
    "            X[:,i] /= np.sum(X[:,i])\n",
    "        weights = np.array([np.array(i)/(sampling_weight[j] if sampling_weight!=None else 1.0) for i,j in zip(X,Z)])\n",
    "        data = []\n",
    "        for i,j in zip(Y,weights):\n",
    "            for k,l in enumerate(j):\n",
    "                data.append([k,i[0]*2,i[1]*2,l])\n",
    "        data = np.array(data)\n",
    "        plt.imshow(_flatmap)\n",
    "        a = seaborn.kdeplot(data[:,1],data[:,2],weights=data[:,-1],hue=[region_list[int(i)] for i in data[:,0]],thresh=0.85,fill=True,level=5)\n",
    "        a.imshow(flatmap_mask,zorder=1)\n",
    "        a.imshow(flatmap,zorder=2)\n",
    "        plt.xlim(300,2700)\n",
    "        plt.ylim(2700,300)\n",
    "        plt.title(name)\n",
    "        plt.axis('off')\n",
    "        plt.axis('off')\n",
    "        plt.axis(\"equal\")\n",
    "        plt.savefig(save_path+name+'.pdf',format='pdf')\n",
    "        plt.savefig(save_path+name+'.png',format='png')\n",
    "        plt.show()\n",
    "    else:\n",
    "        weights = np.array([i/(sampling_weight[j] if sampling_weight!=None else 1.0) for i,j in zip(X,Z)])\n",
    "        plt.imshow(_flatmap)\n",
    "        a = seaborn.kdeplot(Y[:,0]*2,Y[:,1]*2,weights=weights,fill=True,cmap=cmap,common_norm=True,cumulative=False)\n",
    "        a.imshow(flatmap_mask,zorder=1)\n",
    "        a.imshow(flatmap,zorder=2)\n",
    "        plt.xlim(300,2700)\n",
    "        plt.ylim(2700,300)\n",
    "        plt.title(name)\n",
    "        plt.axis('off')\n",
    "        plt.axis('off')\n",
    "        plt.axis(\"equal\")\n",
    "        plt.savefig(save_path+name+'.pdf',format='pdf')\n",
    "        plt.savefig(save_path+name+'.png',format='png')\n",
    "        plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# overall chapter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "overall_path = data_path+'overall/'\n",
    "create_dir(overall_path)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## region visualization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "BS = ['MB','P','MY']\n",
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "for i in BS:\n",
    "    neuronvis.clear(root=True)\n",
    "    for j in BS:\n",
    "        if j==i:\n",
    "            continue\n",
    "        neuronvis.addRegion(j)\n",
    "    neuronvis.addRegion(i,[1,0,0])\n",
    "    set_view(neuronvis,'left')\n",
    "    neuronvis.render.savepng(overall_path+'region_'+i+'.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## soma visualization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = []\n",
    "Y = []\n",
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear()\n",
    "for i in tool_bar(cortex_neuron_name,desc='iteration on neurons (generating shaft) ...',leave=False,colour='#55AF7B'):\n",
    "    X.append(neuron_to_brainstem_information[i]['soma_position'])\n",
    "    Y.append(color_pool[3].tolist())\n",
    "draw_points(neuronvis,X,Y)\n",
    "neuronvis.render.setPointSize(3)\n",
    "for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "    set_view(neuronvis,i)\n",
    "    neuronvis.render.savepng(overall_path+'soma_'+i+'.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = []\n",
    "Y = []\n",
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "for i in tool_bar(cortex_neuron_name,desc='iteration on neurons (generating shaft) ...',leave=False,colour='#55AF7B'):\n",
    "    if neuron_to_brainstem_information[i]['cortex_region'] not in ['AUD']:\n",
    "        continue\n",
    "    X.append(neuron_to_brainstem_information[i]['soma_position'].copy())\n",
    "    if X[-1][2]>5700:\n",
    "        X[-1][2] = 11400-X[-1][2]\n",
    "    Y.append(color_pool[3].tolist())\n",
    "draw_points(neuronvis,X,Y)\n",
    "neuronvis.addRegion('AUD')\n",
    "neuronvis.render.setPointSize(8)\n",
    "neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp = _flatmap*1\n",
    "for i in tool_bar(cortex_neuron_name,desc='iteration on neurons (generating shaft) ...',leave=False,colour='#55AF7B'):\n",
    "        l = neuron_to_brainstem_information[i]['soma_flatten_position']\n",
    "        cv.circle(tmp,[int(l[0])*2, int(l[1])*2],8,np.array(color_pool[3])*255,-1)\n",
    "fig = plt.figure(figsize=(10,10))\n",
    "plt.imshow(tmp)\n",
    "plt.xlim(300,2700)\n",
    "plt.ylim(2700,300)\n",
    "plt.axis(\"equal\")\n",
    "plt.axis('off')\n",
    "plt.title('Soma distribution of the PT neurons',fontsize=fontsize_title)\n",
    "plt.savefig(overall_path+'soma_flattened.pdf',format='pdf')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## axon visualization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuron_color_mapping = []\n",
    "name = []\n",
    "for i in tool_bar(cortex_neuron_name,desc='iteration on neurons (generating mapping colors) ...',leave=False,colour='#55AF7B'):\n",
    "    neuron_color_mapping.append(neuron_to_brainstem_information[i]['soma_flatten_position'])\n",
    "    name.append(i)\n",
    "tmp = position_color_mapping(np.array(neuron_color_mapping),2)\n",
    "neuron_color_mapping = {}\n",
    "for i,j in zip(name,tmp):\n",
    "    neuron_color_mapping[i] = j\n",
    "tmp = _flatmap*1\n",
    "for i in tool_bar(cortex_neuron_name,desc='iteration on neurons (generating shaft) ...',leave=False,colour='#55AF7B'):\n",
    "        l = neuron_to_brainstem_information[i]['soma_flatten_position']\n",
    "        cv.circle(tmp,[int(l[0])*2, int(l[1])*2],8,np.array(neuron_color_mapping[i])*255,-1)\n",
    "fig = plt.figure(figsize=(10,10))\n",
    "plt.imshow(tmp)\n",
    "plt.xlim(flatten_map_r[0],flatten_map_r[1])\n",
    "plt.ylim(flatten_map_r[2],flatten_map_r[3])\n",
    "plt.axis(\"equal\")\n",
    "plt.axis('off')\n",
    "plt.title('Soma position coding',fontsize=fontsize_title)\n",
    "plt.savefig(overall_path+'soma_flattened_coding.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear()\n",
    "for i in tool_bar(name,desc='iteration on neurons (generating dendrite) ...',leave=False,colour='#55AF7B'):\n",
    "    control_console_output(0)\n",
    "    neuron_tree = iondata.getNeuronTreeByID(i[:-7],i[-7:])\n",
    "    neuronvis.addNeuronByID(i[:-7],i[-7:],color=neuron_color_mapping[i].tolist(),somaHide=True,dendriteHide=False,axonHide=True)\n",
    "    control_console_output(1)\n",
    "for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "    set_view(neuronvis,i)\n",
    "    neuronvis.render.savepng(overall_path+'dendrite_'+i+'.png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear()\n",
    "for i in tool_bar(name,desc='iteration on neurons (generating dendrite) ...',leave=False,colour='#55AF7B'):\n",
    "    control_console_output(0)\n",
    "    neuron_tree = iondata.getNeuronTreeByID(i[:-7],i[-7:])\n",
    "    neuronvis.addNeuronByID(i[:-7],i[-7:],color=neuron_color_mapping[i].tolist(),somaHide=True,dendriteHide=False,axonHide=True)\n",
    "    control_console_output(1)\n",
    "for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "    set_view(neuronvis,i)\n",
    "    neuronvis.render.savepng(overall_path+'dendrite_'+i+'.png')\n",
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear()\n",
    "for i in tool_bar(name,desc='iteration on neurons (generating axon) ...',leave=False,colour='#55AF7B'):\n",
    "    control_console_output(0)\n",
    "    neuron_tree = iondata.getNeuronTreeByID(i[:-7],i[-7:])\n",
    "    neuronvis.addNeuronByID(i[:-7],i[-7:],color=neuron_color_mapping[i].tolist(),somaHide=True,dendriteHide=True)\n",
    "    control_console_output(1)\n",
    "for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "    set_view(neuronvis,i)\n",
    "    neuronvis.render.savepng(overall_path+'axon_'+i+'.png')\n",
    "mode = 1\n",
    "angle_threshold = 45\n",
    "length_ratio_threshold = 0.8\n",
    "length_threshold = -1\n",
    "return_nonshaft = False\n",
    "mirror = False\n",
    "final_lines = []\n",
    "final_colors = []\n",
    "final_lines2 = []\n",
    "final_colors2 = []\n",
    "for i in tool_bar(name,desc='iteration on neurons (generating shaft) ...',leave=False,colour='#55AF7B'):\n",
    "    control_console_output(0)\n",
    "    neuron_tree = iondata.getNeuronTreeByID(i[:-7],i[-7:])\n",
    "    control_console_output(1)\n",
    "    [[lines,lines_color],[lines2,lines_color2]] = get_shaft(neuron_tree,shaft_color=neuron_color_mapping[i].tolist(),mode=mode,return_nonshaft=return_nonshaft,angle_threshold=45,length_ratio_threshold=length_ratio_threshold,length_threshold=length_threshold,mirror=mirror)\n",
    "    final_lines += lines\n",
    "    final_colors += lines_color\n",
    "    final_lines2 += lines2\n",
    "    final_colors2 += lines_color2\n",
    "neuronvis.clear()\n",
    "draw_lines(neuronvis,final_lines,final_colors)\n",
    "if return_nonshaft:\n",
    "        draw_lines(neuronvis,final_lines2,final_colors2)\n",
    "for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "    set_view(neuronvis,i)\n",
    "    neuronvis.render.savepng(overall_path+'shaft_'+i+'.png')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## region visualization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear()\n",
    "for i,j in enumerate(['MB','P','MY']):\n",
    "    neuronvis.addRegion(j,color=i)\n",
    "for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "    set_view(neuronvis,i)\n",
    "    neuronvis.render.savepng(overall_path+'braisnte,m_position_'+i+'.png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = [['STR'],['PAL'],['TH','HY'],['MB','P','MY']]\n",
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "set_view(neuronvis,'left')\n",
    "neuronvis.clear(root=True)\n",
    "for i,c in zip(target,[3,2,0,6]):\n",
    "    for j in i:\n",
    "        neuronvis.addRegion(j,color_pool[c])\n",
    "neuronvis.render.savepng(overall_path+'region_visualization_together.png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = [['STR'],['PAL'],['TH','HY'],['MB','P','MY']]\n",
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "set_view(neuronvis,'left')\n",
    "for k in target:\n",
    "    neuronvis.clear(root=True)\n",
    "    for i in target:\n",
    "        for j in i:\n",
    "            neuronvis.addRegion(j)\n",
    "    for i in k:\n",
    "        neuronvis.addRegion(i,[1,0,0])\n",
    "    neuronvis.render.savepng(overall_path+'region_visualization_'+'_'.join(k)+'.png')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## example visualization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "for i,j in zip(['220195023.swc','220195070.swc'],[0,3]):\n",
    "    neuronvis.clear(root=True)\n",
    "    neuron_tree = iondata.getNeuronTreeByID(i[:-7],i[-7:])\n",
    "    neuron_tree.somaRadius = 100\n",
    "    neuronvis.addNeuronTree(neuron_tree,axonColor=color_pool[j],somaColor=[0.5,0.5,0.5],dendriteColor=[0.5,0.5,0.5],mirrorToRight=True,isLine=False)\n",
    "    for k in ['anterior']:\n",
    "        set_view(neuronvis,k)\n",
    "        neuronvis.render.savepng(overall_path+'neuron_examples_'+i+'.png')\n",
    "    neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## sample distribution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize=(5,5))\n",
    "plt.pie([len(sample_info[i]['neurons']) for i in sample_info],labels=[i for i in sample_info],radius=1,startangle=90,colors=color_pool)\n",
    "plt.pie([100],colors=['w'],radius=0.5)\n",
    "plt.title('PT neurons sample distribution',fontsize=fontsize_title)\n",
    "plt.savefig(overall_path+'PT_neurons_sample_distribution.pdf',format='pdf')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## registrition accuracy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.random.choice(list(sample_info.keys()),10,replace=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from scipy import ndimage\n",
    "a,header = nrrd.read(data_path+'accuracy/structure19-20.nrrd')\n",
    "if a.shape[0]==1140:\n",
    "        a = a[::2,::2,::2]\n",
    "standar_mass_center = {}\n",
    "for i in np.unique(a):\n",
    "    standar_mass_center[i] = ndimage.center_of_mass(a==i)\n",
    "standar_mass_center[-1] = ndimage.center_of_mass(a>0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "standar_mass_center"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "deviation = {}\n",
    "for sample in ['220198','221511','212075','220192','221705','221073','221719','221621','233379','231566']:\n",
    "    b,header = nrrd.read(data_path+'accuracy/'+sample+'ch00ds_reg.nrrd')\n",
    "    if b.shape[0]==1140:\n",
    "        b = b[::2,::2,::2]\n",
    "    deviation[sample]={}\n",
    "    for i in np.unique(a):\n",
    "        print(f'{sample}_{i}_{np.array(ndimage.center_of_mass(b==i))}')\n",
    "        deviation[sample][i] = np.linalg.norm(np.array(standar_mass_center[i])-np.array(ndimage.center_of_mass(b==i)))\n",
    "    deviation[sample][-1] = np.linalg.norm(np.array(standar_mass_center[-1])-np.array(ndimage.center_of_mass(b>0)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "transfer_name = {128:'Outline',200:'HIP',230:'FX',100:'ISO',170:'TH_HY',180:'ACT',40:'VL',170:'V4',60:'CP',60:'VIIn',255:'Other',-1:'All'}\n",
    "fig = plt.figure(figsize=(9,4))\n",
    "ax = fig.add_subplot(1,1,1)\n",
    "data = [[deviation[j][i] for j in deviation] for i in transfer_name]\n",
    "#ax.bar(range(1,len(transfer_name)+1),np.median(data,axis=-1),color=color_pool[0],width=0.35)\n",
    "ax.boxplot(data,meanline=True)\n",
    "_ = ax.set_xticks(range(1,len(transfer_name)+1),transfer_name.values())#,fontsize=fontsize_ticks)\n",
    "#ax.set_ylim(0,100)\n",
    "ax.set_xlabel('Region')#,fontsize=fontsize_label)\n",
    "ax.set_ylabel('Deviation (\\u00B5m)',)#fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "ax.set_title('Registration deviation')#,fontsize=fontsize_title)\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(overall_path+'Registration_deviation.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a,header = nrrd.read(data_path+'accuracy/structure19-20.nrrd')\n",
    "if a.shape[0]==1140:\n",
    "        a = a[::2,::2,::2]\n",
    "accuracy = {}\n",
    "for sample in ['220198','221511','212075','220192','221705','221073','221719','221621','233379','231566']:\n",
    "    b,header = nrrd.read(data_path+'accuracy/'+sample+'ch00ds_reg.nrrd')\n",
    "    if b.shape[0]==1140:\n",
    "        b = b[::2,::2,::2]\n",
    "    accuracy[sample]={}\n",
    "    for i in np.unique(a):\n",
    "        x = (a==i)\n",
    "        y = (b==i)\n",
    "        accuracy[sample][i] = np.sum(((x+y)>0)*(x==y))/np.sum((x+y)>0)\n",
    "    accuracy[sample][-1] = np.sum(((a+b)>0)*(a==b))/np.sum((a+b)>0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "transfer_name = {128:'Outline',200:'HIP',230:'FX',100:'ISO',170:'TH_HY',180:'ACT',40:'VL',170:'V4',60:'CP',60:'VIIn',255:'Other',-1:'All'}\n",
    "fig = plt.figure(figsize=(9,4))\n",
    "ax = fig.add_subplot(1,1,1)\n",
    "data = [[accuracy[j][i]*100 for j in accuracy] for i in transfer_name]\n",
    "#ax.bar(range(1,len(transfer_name)+1),np.median(data,axis=-1),color=color_pool[0],width=0.35)\n",
    "ax.boxplot(data,meanline=True)\n",
    "_ = ax.set_xticks(range(1,len(transfer_name)+1),transfer_name.values(),fontsize=fontsize_ticks)\n",
    "ax.set_ylim(0,100)\n",
    "ax.set_xlabel('Region',fontsize=fontsize_label)\n",
    "ax.set_ylabel('Accuracy',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "ax.set_title('Registration accuracy',fontsize=fontsize_title)\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(overall_path+'Registration_accuracy.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=False)\n",
    "for i,j in zip(['221225189.swc'],[0]):\n",
    "    neuron_tree = iondata.getNeuronTreeByID(i[:-7],i[-7:])\n",
    "    neuron_tree.somaRadius = 100\n",
    "    neuronvis.addNeuronTree(neuron_tree,axonColor=color_pool[j],somaColor=[0.5,0.5,0.5],dendriteColor=[0.5,0.5,0.5],mirrorToRight=True,isLine=False)\n",
    "for k in ['anterior','dorsal','left','right']:\n",
    "    set_view(neuronvis,k)\n",
    "    neuronvis.render.savepng(overall_path+'registration_examples_'+i+'_'+k+'.png')\n",
    "neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## gene distribution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gene_distribution = {}\n",
    "for i in sample_info:\n",
    "    if sample_info[i]['gene'] not in gene_distribution:\n",
    "        gene_distribution[sample_info[i]['gene']] = 0\n",
    "    gene_distribution[sample_info[i]['gene']] += len(sample_info[i]['neurons'])\n",
    "tmp = list(gene_distribution.keys())\n",
    "gene_distribution['others'] = 0\n",
    "_sum = np.sum(list(gene_distribution.values()))\n",
    "for i in tmp:\n",
    "    if (gene_distribution[i]/_sum)<0.1:\n",
    "        gene_distribution['others'] += gene_distribution[i]\n",
    "        del gene_distribution[i]\n",
    "plt.figure(figsize=(5,5))\n",
    "def func(pct, allvals):\n",
    "    absolute = int(np.round(pct/100.*np.sum(allvals)))\n",
    "    return f\"{pct:.1f}%\\n({absolute:d})\"\n",
    "plt.pie(gene_distribution.values(),labels=gene_distribution.keys(),radius=1,startangle=90,colors=color_pool[0:4],explode=[0.05 for i in gene_distribution], autopct=lambda pct: func(pct,list(gene_distribution.values())))\n",
    "plt.pie([10],colors=['w'],radius=0.4)\n",
    "plt.title('Gene distribution',fontsize=fontsize_title)\n",
    "plt.savefig(overall_path+'PT_neurons_gene_distribution.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## region distribution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp = {}\n",
    "error = 0\n",
    "for i in cortex_neuron_name:\n",
    "    region = neuron_to_brainstem_information[i]['cortex_region']\n",
    "    if region not in tmp:\n",
    "        tmp[region] = 0\n",
    "    tmp[region] += 1\n",
    "x = cortex_region\n",
    "y = [tmp[i] if i in tmp else 0 for i in x]\n",
    "plt.figure(figsize=(5,5))\n",
    "plt.pie(y,labels=x,radius=1,startangle=90,colors=color_pool[0:4],explode=[0.05 for i in y],wedgeprops=_wedgeprops)\n",
    "plt.pie([100],colors=['w'],radius=0.4,wedgeprops=_wedgeprops)\n",
    "plt.title('PT neurons region distribution',fontsize=fontsize_title)\n",
    "plt.savefig(overall_path+'PT_neurons_region_distribution.pdf',format='pdf')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp = {}\n",
    "error = 0\n",
    "for i in cortex_neuron_name:\n",
    "    region = neuron_to_brainstem_information[i]['cortex_region']\n",
    "    if region not in tmp:\n",
    "        tmp[region] = 0\n",
    "    tmp[region] += 1\n",
    "x = ['FRP','PL','ILA','ORB','AI','GU','VISC','MOs','MOp','SSp','SSs','VIS','PTLp','RSP','ACA','AUD','TEa','ECT','PERI']\n",
    "y = [tmp[i] if i in tmp else 0 for i in x]\n",
    "plt.figure(figsize=(10,5))\n",
    "plt.bar(range(len(y)),y,width=0.25,color=color_pool[0])\n",
    "plt.xticks(range(len(x)),x,fontsize=fontsize_ticks)\n",
    "plt.xlabel('Region',fontsize=fontsize_label)\n",
    "plt.ylabel('Number',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.title('PT neurons region distribution',fontsize=fontsize_title)\n",
    "plt.savefig(overall_path+'PT_neurons_region_distribution.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## soma depth distribution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp = {}\n",
    "error = 0\n",
    "for i in cortex_neuron_name:\n",
    "    region = neuron_to_brainstem_information[i]['cortex_region']\n",
    "    depth = neuron_to_brainstem_information[i]['soma_depth']*20\n",
    "    if region not in tmp:\n",
    "        tmp[region] = []\n",
    "    if depth!=0:\n",
    "        tmp[region].append(depth)\n",
    "    else:\n",
    "        error += 1\n",
    "print('finished, error rate: ',error,',',np.round(error/len(cortex_neuron_name)*100,1),'%')\n",
    "parameter=[0,1200,1,15]\n",
    "X = []\n",
    "for i in tmp:\n",
    "    X += tmp[i]\n",
    "distribution_discrete,distribution_continuous = estimate_density_function(X,parameter)\n",
    "plt.figure(figsize=(3,5))\n",
    "plt.plot(distribution_continuous,range(len(distribution_continuous)),color=color_pool_hex[0])\n",
    "plt.barh(range(parameter[1]),distribution_discrete,color=color_pool_hex[3],height=0.75)\n",
    "plt.axhline(np.mean(X),linestyle='--',color='#555555',linewidth=3)\n",
    "plt.ylim(parameter[1],parameter[0])\n",
    "plt.xlim(0,0.004)\n",
    "plt.xlabel('Probability',fontsize=fontsize_label)\n",
    "plt.ylabel('Depth (\\u00B5m)',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.title('Depth distribution',fontsize=fontsize_title)\n",
    "plt.savefig(overall_path+'PT_neurons_soma_depth_distribution.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()\n",
    "print('mean: ',np.mean(X))\n",
    "print('mean: ',np.std(X))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## soma depth distribution by each level"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = ['CTX','CNU','HY','TH','MB','P','MY','unknow']\n",
    "info = {}\n",
    "error = 0\n",
    "for i in cortex_neuron_name:\n",
    "    region = neuron_to_brainstem_information[i]['cortex_region']\n",
    "    depth = neuron_to_brainstem_information[i]['soma_depth']*20\n",
    "    if depth!=0:\n",
    "        info[i] = [depth,get_axonal_statics_by_regions(i,target,'terminalregion')]\n",
    "        tmp = list(info[i][1])\n",
    "        tmp = tmp[:3]+[0]+tmp[3:]\n",
    "        tmp[2] -= neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][brainstem_region.index('ZI')]\n",
    "        tmp[4] -= neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][brainstem_region.index('RT')]\n",
    "        tmp[3] += neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][brainstem_region.index('ZI')]+neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][brainstem_region.index('RT')]\n",
    "        info[i][1] = np.array(tmp)\n",
    "    else:\n",
    "        error += 1\n",
    "print('finished, error rate: ',error,',',np.round(error/len(cortex_neuron_name)*100,1),'%')\n",
    "target = ['CTX','CNU','HY','PTH','TH','MB','P','MY','unknow']\n",
    "parameter=[0,1200,1,35]\n",
    "D = []\n",
    "W = []\n",
    "for i in info:\n",
    "    D.append(info[i][0])\n",
    "    W.append(info[i][1])\n",
    "D = np.array(D)\n",
    "W = np.array(W)\n",
    "fig = plt.figure(figsize=(3*3,5))\n",
    "for i,j in enumerate(target[2:]):\n",
    "    ax = fig.add_subplot(1,7,i+1)\n",
    "    distribution_discrete,distribution_continuous = estimate_density_function(D,parameter,weight=W[:,target.index(j)])\n",
    "    ax.plot(distribution_continuous,range(len(distribution_continuous)),color=color_pool_hex[0])\n",
    "    ax.barh(range(parameter[1]),distribution_discrete,color=color_pool_hex[3],height=0.25)\n",
    "    ax.axhline(np.mean(distribution_continuous*np.array([ii for ii in range(len(distribution_continuous))]))*len(distribution_continuous),linestyle='--',color='#555555',linewidth=3)\n",
    "    ax.set_ylim(parameter[1],parameter[0])\n",
    "    ax.set_xlim(0,0.004)\n",
    "    if i==2:\n",
    "        ax.set_xlabel('Probability',fontsize=fontsize_label)\n",
    "    if i==0:\n",
    "        ax.set_ylabel('Depth (\\u00B5m)',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "    if i>0:\n",
    "        ax.set_yticks([],[])\n",
    "    ax.set_xticks([0,0.004],['0','4e-3'])\n",
    "    ax.set_title(j if j!='unknow' else 'Spine',fontsize=fontsize_label*0.75)\n",
    "    #print('mean: ',np.mean(X[:,target.index(j)]))\n",
    "plt.suptitle('Depth distribution',fontsize=fontsize_title)\n",
    "plt.savefig(overall_path+'PT_neurons_soma_depth_distribution_by_level.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = ['CTX','CNU','HY','TH','MB','P','MY','unknow']\n",
    "info = {}\n",
    "error = 0\n",
    "for i in cortex_neuron_name:\n",
    "    region = neuron_to_brainstem_information[i]['cortex_region']\n",
    "    depth = neuron_to_brainstem_information[i]['soma_depth']*20\n",
    "    if depth!=0:\n",
    "        info[i] = [depth,get_axonal_statics_by_regions(i,target,'terminalregion')]\n",
    "        tmp = list(info[i][1])\n",
    "        tmp = tmp[:3]+[0]+tmp[3:]\n",
    "        tmp[2] -= neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][brainstem_region.index('ZI')]\n",
    "        tmp[4] -= neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][brainstem_region.index('RT')]\n",
    "        tmp[3] += neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][brainstem_region.index('ZI')]+neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][brainstem_region.index('RT')]\n",
    "        info[i][1] = np.array(tmp)\n",
    "    else:\n",
    "        error += 1\n",
    "print('finished, error rate: ',error,',',np.round(error/len(cortex_neuron_name)*100,1),'%')\n",
    "target = ['CTX','CNU','HY','PTH','TH','MB','P','MY','unknow']\n",
    "parameter=[0,1200,1,35]\n",
    "D = []\n",
    "W = []\n",
    "for i in info:\n",
    "    D.append(info[i][0])\n",
    "    W.append(info[i][1])\n",
    "D = np.array(D)\n",
    "W = np.array(W)\n",
    "parameter=[0,1200,1,55]\n",
    "fig = plt.figure(figsize=(3,5))\n",
    "for i,j in enumerate(target[2:]):\n",
    "    distribution_discrete,distribution_continuous = estimate_density_function(D,parameter,weight=W[:,target.index(j)])\n",
    "    plt.plot(distribution_continuous,range(len(distribution_continuous)),label=j)\n",
    "plt.legend()\n",
    "plt.ylim(parameter[1],parameter[0])\n",
    "plt.xlim(0,0.004)\n",
    "plt.xlabel('Probability',fontsize=fontsize_label)\n",
    "plt.ylabel('Depth (\\u00B5m)',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.xticks([0,0.004],['0','4e-3'])\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(overall_path+'PT_neurons_soma_depth_distribution_together.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mask = (get_region_mask('MB')+get_region_mask('P')+get_region_mask('MY'))>0\n",
    "A = []\n",
    "B = []\n",
    "for neuron in neuron_terminals:\n",
    "    depth = neuron_to_brainstem_information[neuron]['soma_depth']\n",
    "    if depth==0:\n",
    "        continue\n",
    "    tmp = []\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            tmp.append(terminal[0])\n",
    "    if len(tmp)==0:\n",
    "        continue\n",
    "    A.append(depth)\n",
    "    B.append(np.mean(tmp))\n",
    "plt.scatter(A,B,s=1)\n",
    "plt.show()\n",
    "from scipy import stats\n",
    "stats.pearsonr(np.array(A).astype(float)*20,np.array(B).astype(float)*0.1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## axon length and terminal difference"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "transfer_name = {128:'Outline',200:'HIP',230:'FX',100:'ISO',170:'TH_HY',180:'ACT',40:'VL',170:'V4',60:'CP',60:'VIIn',255:'Other',-1:'All'}\n",
    "data = []\n",
    "for i in tool_bar(cortex_neuron_name,desc='iteration on neurons (generating mapping colors) ...',leave=False,colour='#55AF7B'):\n",
    "    data.append([np.sum(neuron_to_brainstem_information[i]['axonal_projection_terminal_combined']),np.sum(neuron_to_brainstem_information[i]['axonal_projection_length_combined'])])\n",
    "data = np.array(data)\n",
    "fig = plt.figure(figsize=(5,5))\n",
    "ax = fig.add_subplot(1,1,1)\n",
    "ax.scatter(data[:,1],data[:,0],s=1)\n",
    "\n",
    "from scipy import stats\n",
    "r = stats.pearsonr(data[:,1],data[:,0])\n",
    "coefficients = np.polyfit(data[:,1],data[:,0],1)\n",
    "print('pearson_r: ',r,'\\n',\n",
    "'coefficients: ',coefficients)\n",
    "plt.axline([0,coefficients[1]],slope=coefficients[0],color=color_pool[0],linestyle='dashed')\n",
    "\n",
    "ax.set_xlabel('Axon length (\\u00B5m)',fontsize=fontsize_label)\n",
    "ax.set_ylabel('Terminal number',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "ax.set_title('Axon-Terminal relation',fontsize=fontsize_title)\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(overall_path+'Axon_terminal_relationship.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A = []\n",
    "B = []\n",
    "for i in tool_bar(cortex_neuron_name,desc='iteration on neurons (generating mapping colors) ...',leave=False,colour='#55AF7B'):\n",
    "    A.append(get_axonal_statics_by_regions(i,['CTX','CNU','HY','TH','MB','P','MY','unknow'],'terminalregion'))\n",
    "    B.append(get_axonal_statics_by_regions(i,['CTX','CNU','HY','TH','MB','P','MY','unknow'],'projectregion'))\n",
    "    A[-1][-1] = neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][brainstem_region.index('unknow')]\n",
    "    B[-1][-1] = neuron_to_brainstem_information[i]['axonal_projection_length_combined'][brainstem_region.index('unknow')]\n",
    "tmpp = []\n",
    "for i,j in zip(A,cortex_neuron_name):\n",
    "    tmp = list(i)\n",
    "    tmp = tmp[:3]+[0]+tmp[3:]\n",
    "    tmp[2] -= neuron_to_brainstem_information[j]['axonal_projection_terminal_combined'][brainstem_region.index('ZI')]\n",
    "    tmp[4] -= neuron_to_brainstem_information[j]['axonal_projection_terminal_combined'][brainstem_region.index('RT')]\n",
    "    tmp[3] += neuron_to_brainstem_information[j]['axonal_projection_terminal_combined'][brainstem_region.index('ZI')]+neuron_to_brainstem_information[j]['axonal_projection_terminal_combined'][brainstem_region.index('RT')]\n",
    "    tmpp.append(np.array(tmp))\n",
    "A = np.array(tmpp)\n",
    "tmpp = []\n",
    "for i,j in zip(B,cortex_neuron_name):\n",
    "    tmp = list(i)\n",
    "    tmp = tmp[:3]+[0]+tmp[3:]\n",
    "    tmp[2] -= neuron_to_brainstem_information[j]['axonal_projection_terminal_combined'][brainstem_region.index('ZI')]\n",
    "    tmp[4] -= neuron_to_brainstem_information[j]['axonal_projection_terminal_combined'][brainstem_region.index('RT')]\n",
    "    tmp[3] += neuron_to_brainstem_information[j]['axonal_projection_terminal_combined'][brainstem_region.index('ZI')]+neuron_to_brainstem_information[j]['axonal_projection_terminal_combined'][brainstem_region.index('RT')]\n",
    "    tmpp.append(np.array(tmp))\n",
    "B = np.array(tmpp)\n",
    "plt.figure(figsize=(5,4))\n",
    "plt.bar(range(A.shape[1]),np.sum(A,axis=0)/np.sum(A),width=0.5,label='terminal',color=color_pool[0])\n",
    "plt.bar(range(A.shape[1]),-np.sum(B,axis=0)/np.sum(B),width=0.5,label='length',color=np.array([2,50,196])/255)\n",
    "plt.yticks([-0.5,-0.25,0.0,0.25,0.5],[0.5,0.25,0.0,0.25,0.5])\n",
    "plt.ylim(-0.0,0.5)\n",
    "plt.xticks(range(A.shape[1]),['CTX','CNU','HY','PTH','TH','MB','P','MY','Spine'],fontsize=fontsize_ticks,rotation=-45)\n",
    "plt.xlabel('Target regions',fontsize=fontsize_label)\n",
    "plt.ylabel('Ratio (%)',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.title(\"Terminal distribution\",fontsize=fontsize_title)\n",
    "#plt.legend()\n",
    "plt.savefig(overall_path+'axon_length_terminal_difference.pdf',format='pdf')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def func(pct, allvals):\n",
    "    absolute = int(np.round(pct/100.*np.sum(allvals)))\n",
    "    return f\"{pct:.1f}%\\n({absolute:d})\"\n",
    "x = (np.sum(A,axis=0))[-4:-1]\n",
    "_ = plt.pie(x,labels=['MB','P','MY'], autopct=lambda pct: func(pct,x),textprops=dict(color=\"w\"),colors=color_pool[[0,1,3]])\n",
    "plt.legend()\n",
    "plt.savefig(overall_path+'axon_length_terminal_difference_brainstem.pdf',format='pdf')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def func(pct, allvals):\n",
    "    absolute = int(np.round(pct/100.*np.sum(allvals)))\n",
    "    return f\"{pct:.1f}%\\n({absolute:d})\"\n",
    "x = (np.sum(A,axis=0))\n",
    "_ = plt.pie(x,labels=['CTX','CNU','HY','PTH','TH','MB','P','MY','Spine'], autopct=lambda pct: func(pct,x),textprops=dict(color=\"w\"),colors=color_pool)\n",
    "plt.legend()\n",
    "plt.savefig(overall_path+'axon_length_terminal_difference_all.pdf',format='pdf')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x/np.sum(x)*100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.sum(np.round(x/np.sum(x)*100,0))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## terminal distribution in each brain level"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp = {}\n",
    "index_list = {\n",
    "    'Hypothalamus':[brainstem_region.index(i) for i in brainstem_region_hierachy['HY']],\n",
    "    'Zonalimitans':[brainstem_region.index(i) for i in ['RT','ZI']],\n",
    "    'Thlamus':[brainstem_region.index(i) for i in brainstem_region_hierachy['TH']],\n",
    "    'Pretectum':[brainstem_region.index(i) for i in ['APN','MPT','NOT','NPC','OP','PPT','RPF']],\n",
    "    'Midbrain':[brainstem_region.index(i) for i in brainstem_region_hierachy['MB']],\n",
    "    'Pons':[brainstem_region.index(i) for i in brainstem_region_hierachy['P']],\n",
    "    'Medulla_spine':[brainstem_region.index(i) for i in brainstem_region_hierachy['MY']+['unknow']],\n",
    "}\n",
    "index_list['Hypothalamus'].pop(index_list['Hypothalamus'].index(brainstem_region.index('ZI')))\n",
    "index_list['Thlamus'].pop(index_list['Thlamus'].index(brainstem_region.index('RT')))\n",
    "for i in ['APN','MPT','NOT','NPC','OP','PPT','RPF']:\n",
    "    index_list['Midbrain'].pop(index_list['Midbrain'].index(brainstem_region.index(i)))\n",
    "for i in neuron_to_brainstem_information:\n",
    "    region = neuron_to_brainstem_information[i]['cortex_region']\n",
    "    if region not in tmp:\n",
    "        tmp[region] = {}\n",
    "    for j in index_list:\n",
    "        if j not in tmp[region]:\n",
    "            tmp[region][j] = []\n",
    "        tmp[region][j].append([neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][k] for k in index_list[j]])\n",
    "result = []\n",
    "for i in tmp:\n",
    "    result.append([])\n",
    "    for j in tmp[i]:\n",
    "        result[-1] += np.mean(tmp[i][j],axis=0).tolist()\n",
    "result = np.array(result)\n",
    "target_label = []\n",
    "for i in index_list:\n",
    "    for j in index_list[i]:\n",
    "        target_label.append(brainstem_region[j])\n",
    "target_label[-1] = 'spine'\n",
    "source_label = list(tmp.keys())\n",
    "colors = np.array([(255, 0, 0), (255, 80, 0), (255, 161, 0), (255, 241, 0), (187, 255, 0), (107, 255, 0), (26, 255, 0), (0, 255, 53), (0, 255, 134), (0, 255, 214), (0, 214, 255), (0, 134, 255), (0, 53, 255), (26, 0, 255), (107, 0, 255), (187, 0, 255), (255, 0, 241), (255, 0, 161), (255, 0, 80)])/255\n",
    "fig = plt.figure(figsize=(35,8))\n",
    "ax = fig.add_subplot(1,1,1)\n",
    "tmp = ['MOp','MOs','SSp','SSs','RSP','ACA','PTLp','VIS','AUD','TEa','ECT','PERI','GU','VISC','AI','ILA','PL','FRP','ORB']\n",
    "for i,j in enumerate(result):\n",
    "    p = np.array([[tmp.index(source_label[i]),k] for k in range(len(j))])\n",
    "    ax.scatter(p[:,1],p[:,0],s=j*2.5,color=colors[tmp.index(source_label[i])])\n",
    "source_label = tmp\n",
    "n = 0\n",
    "for i in list(index_list.keys())[:-1]:\n",
    "    n += len(index_list[i])\n",
    "    ax.axvline(n-0.5,linestyle='--',color='red',linewidth=1)\n",
    "for i in range(len(source_label)):\n",
    "    ax.axhline(i,linestyle='--',color='#AAAAAA',linewidth=0.5)\n",
    "_ = ax.set_yticks(range(len(source_label)),source_label,rotation=0,fontsize=fontsize_ticks)\n",
    "_ = ax.set_xticks(range(len(target_label)),target_label,rotation=90,fontsize=fontsize_ticks)\n",
    "ax.set_xlim(-1,len(target_label)+1)\n",
    "ax.set_xlabel('Subcortical target',fontsize=fontsize_label)\n",
    "ax.set_ylabel('Cortical source',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "ax.set_title('Projection matrix (terminal number)',fontsize=fontsize_title)\n",
    "a = {}\n",
    "for i in [50,100,200]:\n",
    "    a[i] = ax.scatter([-10],[2],s=i,c='#AAAAAA')\n",
    "ax.legend(a.values(),['n='+str(i) for i in a.keys()],loc='upper right')\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(overall_path+'Terminal_distribution_in_subcortex.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp = {}\n",
    "index_list = {\n",
    "    'Pretectum':[brainstem_region.index(i) for i in ['APN','MPT','NOT','NPC','OP','PPT','RPF']],\n",
    "    'Midbrain':[brainstem_region.index(i) for i in brainstem_region_hierachy['MB']],\n",
    "    'Pons':[brainstem_region.index(i) for i in brainstem_region_hierachy['P']],\n",
    "    'Medulla_spine':[brainstem_region.index(i) for i in brainstem_region_hierachy['MY']+['unknow']],\n",
    "}\n",
    "for i in ['APN','MPT','NOT','NPC','OP','PPT','RPF']:\n",
    "    index_list['Midbrain'].pop(index_list['Midbrain'].index(brainstem_region.index(i)))\n",
    "for i in neuron_to_brainstem_information:\n",
    "    region = neuron_to_brainstem_information[i]['cortex_region']\n",
    "    if region not in tmp:\n",
    "        tmp[region] = {}\n",
    "    for j in index_list:\n",
    "        if j not in tmp[region]:\n",
    "            tmp[region][j] = []\n",
    "        tmp[region][j].append([neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][k] for k in index_list[j]])\n",
    "result = []\n",
    "for i in tmp:\n",
    "    result.append([])\n",
    "    for j in tmp[i]:\n",
    "        result[-1] += np.mean(tmp[i][j],axis=0).tolist()\n",
    "result = np.array(result)\n",
    "result2 = []\n",
    "target_label = []\n",
    "for i in index_list:\n",
    "    for j in index_list[i]:\n",
    "        target_label.append(brainstem_region[j])\n",
    "target_label2 = []\n",
    "for i,j in enumerate(target_label):\n",
    "    if np.max(result[:,i])>5:\n",
    "        target_label2.append(j)\n",
    "        result2.append(result[:,i])\n",
    "result = np.array(result2).T\n",
    "target_label = target_label2\n",
    "if target_label[-1] == 'unknow':\n",
    "    target_label[-1] = 'spine'\n",
    "source_label = list(tmp.keys())\n",
    "colors = np.array([(255, 0, 0), (255, 80, 0), (255, 161, 0), (255, 241, 0), (187, 255, 0), (107, 255, 0), (26, 255, 0), (0, 255, 53), (0, 255, 134), (0, 255, 214), (0, 214, 255), (0, 134, 255), (0, 53, 255), (26, 0, 255), (107, 0, 255), (187, 0, 255), (255, 0, 241), (255, 0, 161), (255, 0, 80)])/255\n",
    "fig = plt.figure(figsize=(6,8))\n",
    "ax = fig.add_subplot(1,1,1)\n",
    "tmp = ['MOp','MOs','SSp','SSs','RSP','ACA','PTLp','VIS','AUD','TEa','ECT','PERI','GU','VISC','AI','ILA','PL','FRP','ORB']\n",
    "for i,j in enumerate(result):\n",
    "    p = np.array([[tmp.index(source_label[i]),k] for k in range(len(j))])\n",
    "    ax.scatter(p[:,0],p[:,1],s=j*2.5,color=colors[tmp.index(source_label[i])])\n",
    "source_label = tmp\n",
    "n = 0\n",
    "for i in list(index_list.keys())[:-1]:\n",
    "    n += len(index_list[i])\n",
    "    ax.axhline(n-0.5,linestyle='--',color='red',linewidth=1)\n",
    "for i in range(len(source_label)):\n",
    "    ax.axvline(i,linestyle='--',color='#AAAAAA',linewidth=0.5)\n",
    "_ = ax.set_xticks(range(len(source_label)),source_label,fontsize=fontsize_ticks,rotation=-90)\n",
    "_ = ax.set_yticks(range(len(target_label)),target_label,fontsize=fontsize_ticks)\n",
    "ax.set_ylim(len(target_label)+0.5,-0.5)\n",
    "ax.set_xlim(len(source_label)+0.5,-0.5)\n",
    "ax.set_ylabel('Target',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "ax.set_xlabel('Source',fontsize=fontsize_label)\n",
    "ax.set_title('Projection matrix (terminal number)',fontsize=fontsize_title)\n",
    "a = {}\n",
    "for i in [50,100,200]:\n",
    "    a[i] = ax.scatter([-100],[2],s=i,c='#AAAAAA')\n",
    "ax.legend(a.values(),['n='+str(i) for i in a.keys()],loc='upper right')\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(overall_path+'Terminal_distribution_in_subcortex_brief.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp = {}\n",
    "error = 0\n",
    "for i in cortex_neuron_name:\n",
    "    region = neuron_to_brainstem_information[i]['cortex_region']\n",
    "    if region not in tmp:\n",
    "        tmp[region] = 0\n",
    "    tmp[region] += 1\n",
    "x = ['MOp','MOs','SSp','SSs','RSP','ACA','PTLp','VIS','AUD','TEa','ECT','PERI','GU','VISC','AI','ILA','PL','FRP','ORB'][::-1]\n",
    "y = [tmp[i] if i in tmp else 0 for i in x]\n",
    "plt.figure(figsize=(6,2))\n",
    "plt.bar(range(len(y)),y,width=0.25,color=color_pool[0])\n",
    "plt.xticks(range(len(x)),x,fontsize=fontsize_ticks,rotation=-90)\n",
    "plt.xlabel('Region',fontsize=fontsize_label)\n",
    "plt.ylabel('Number',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.title('PT neurons region distribution',fontsize=fontsize_title)\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(overall_path+'PT_neurons_region_distribution.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## brainstem part and its origin"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "index_list = {\n",
    "    'Hypothalamus':[brainstem_region.index(i) for i in brainstem_region_hierachy['HY']],\n",
    "    'Prethalamus':[brainstem_region.index(i) for i in ['RT','ZI']],\n",
    "    'Thlamus':[brainstem_region.index(i) for i in brainstem_region_hierachy['TH']],\n",
    "    'Pretectum':[brainstem_region.index(i) for i in ['APN','MPT','NOT','NPC','OP','PPT','RPF']],\n",
    "    'Midbrain':[brainstem_region.index(i) for i in brainstem_region_hierachy['MB']],\n",
    "    'Pons':[brainstem_region.index(i) for i in brainstem_region_hierachy['P']],\n",
    "    'Medulla_spine':[brainstem_region.index(i) for i in brainstem_region_hierachy['MY']+['unknow']],\n",
    "}\n",
    "index_list['Hypothalamus'].pop(index_list['Hypothalamus'].index(brainstem_region.index('ZI')))\n",
    "index_list['Thlamus'].pop(index_list['Thlamus'].index(brainstem_region.index('RT')))\n",
    "for i in ['APN','MPT','NOT','NPC','OP','PPT','RPF']:\n",
    "    index_list['Midbrain'].pop(index_list['Midbrain'].index(brainstem_region.index(i)))\n",
    "info = {}\n",
    "for i in neuron_to_brainstem_information:\n",
    "    d = neuron_to_brainstem_information[i]['soma_depth']\n",
    "    p = neuron_to_brainstem_information[i]['soma_flatten_position']\n",
    "    if len(p)==0:\n",
    "        continue\n",
    "    info[i] = [d,p]\n",
    "    for j in index_list:\n",
    "        tmp = 0.0\n",
    "        for k in index_list[j]:\n",
    "            tmp += neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][k]\n",
    "        info[i].append(tmp)\n",
    "r = 200\n",
    "s = 75\n",
    "transition_color = np.array([[255,255,255],[161,238,204],[255,242,193],[246,49,141]])/255.0\n",
    "cmap1 = mcolors.LinearSegmentedColormap.from_list('cmap1', transition_color)\n",
    "magic_number = int(np.ceil(len(index_list)/2))\n",
    "fig,axes = plt.subplots(2,magic_number,figsize=(magic_number*5/2,5))\n",
    "kernel = generate_gaussian_kernel(r,s)\n",
    "for i,j in enumerate(index_list):\n",
    "    c = 0.0\n",
    "    tmp = _flatmap[...,0]*0.0\n",
    "    soma_list = [[],[]]\n",
    "    for k in info:\n",
    "        d,p = info[k][:2]\n",
    "        e = info[k][2:]\n",
    "        fill_kernel(tmp,np.array(p[::-1])*2,e[i]/neuron_to_brainstem_information[k]['relative_density'],r,kernel)\n",
    "        soma_list[0].append(np.array(p[::-1])*2)\n",
    "        soma_list[1].append(e[i])\n",
    "    soma_list[1] = np.array(soma_list[1])**0.5\n",
    "    soma_list[1] /= np.max(soma_list[1])\n",
    "    result = np.where(_flatmap[...,0]==-1,0,tmp)\n",
    "    #for k,l in zip(soma_list[0],soma_list[1]):\n",
    "    #    cv.circle(result,[int(np.round(k[1])), int(np.round(k[0]))],int(np.round(l*10))+1,np.array([0.5,0.5,0.5]),-1)\n",
    "    result /= np.max(result)\n",
    "    _ = axes[i//magic_number,i%magic_number].imshow(result,cmap=cmap1,vmin=0,vmax=1)\n",
    "    axes[i//magic_number,i%magic_number].imshow(flatmap_mask,zorder=1)\n",
    "    axes[i//magic_number,i%magic_number].imshow(flatmap2,zorder=2)\n",
    "    axes[i//magic_number,i%magic_number].set_xlim(flatten_map_r[0],flatten_map_r[1])\n",
    "    axes[i//magic_number,i%magic_number].set_ylim(flatten_map_r[2],flatten_map_r[3])\n",
    "    axes[i//magic_number,i%magic_number].set_axis_off()\n",
    "    axes[i//magic_number,i%magic_number].set_title(j)\n",
    "axes[-1,-1].set_axis_off()\n",
    "fig.colorbar(_,ax=axes[-1,-1],extend='both')\n",
    "plt.suptitle('Projection strength')\n",
    "plt.savefig(overall_path+'terminal_origion.pdf',format='pdf',bbox_inches='tight',dpi=500)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## topology relationship"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### topology index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = ['MB','P','MY']\n",
    "info = {}\n",
    "for i in target:\n",
    "    mask = get_region_mask(i)\n",
    "    for id,neuron in enumerate(neuron_to_brainstem_information):\n",
    "        if neuron not in info:\n",
    "            info[neuron] = []\n",
    "        tmp = []\n",
    "        for terminal in neuron_terminals[neuron]['terminals']:\n",
    "            if if_inside_mask(mask,np.array(terminal)/10):\n",
    "                tmp.append(terminal.copy())\n",
    "                if tmp[-1][2]>5700:\n",
    "                    tmp[-1][2] = 11400-tmp[-1][2]\n",
    "        info[neuron].append(np.mean(tmp,axis=0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.linear_model import LinearRegression\n",
    "def _norml(x):\n",
    "    tmp = np.array(x)\n",
    "    return (tmp-np.mean(tmp,axis=0))/np.std(tmp)\n",
    "def KL(X,Y):\n",
    "    a = np.exp(-np.sum((X[:,np.newaxis,:]-X[np.newaxis,:])**2,axis=-1))\n",
    "    b = np.exp(-np.sum((Y[:,np.newaxis,:]-Y[np.newaxis,:])**2,axis=-1))\n",
    "    a /= np.sum(a,axis=-1)[:,np.newaxis]\n",
    "    b /= np.sum(b,axis=-1)[:,np.newaxis]\n",
    "    result = 0\n",
    "    for i in range(len(X)):\n",
    "        for j in range(len(X)):\n",
    "            if i==j:\n",
    "                continue\n",
    "            result += a[i,j]*np.log(a[i,j]/b[i,j])\n",
    "    return result/len(X)\n",
    "S = np.array([neuron_to_brainstem_information[i]['soma_position'] for i in neuron_to_brainstem_information])\n",
    "for i,j in enumerate(target):\n",
    "    tmp = {'dorsal':[[],[]],'ventral':[[],[]]}\n",
    "    for k,l in enumerate(neuron_to_brainstem_information):\n",
    "        if np.sum(info[l][i])>0:\n",
    "            if S[k][1]<3000:\n",
    "                m = 'dorsal'\n",
    "            else:\n",
    "                m = 'ventral'\n",
    "            tmp[m][0].append(S[k])\n",
    "            tmp[m][1].append(info[l][i])\n",
    "    for k in tmp:\n",
    "        tmp[k][0] = _norml(tmp[k][0])\n",
    "        tmp[k][1] = _norml(tmp[k][1])\n",
    "        #linearModel = LinearRegression()\n",
    "        #linearModel.fit(tmp[k][0],tmp[k][1])\n",
    "        #print(k,linearModel.score(tmp[k][0],tmp[k][1]))\n",
    "        print(k,KL(tmp[k][0],tmp[k][1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def _norml(x):\n",
    "    tmp = np.array(x)\n",
    "    return (tmp-np.mean(tmp,axis=0))/np.std(tmp)\n",
    "def KL(X,Y):\n",
    "    a = np.exp(-np.sum((X[:,np.newaxis,:]-X[np.newaxis,:])**2,axis=-1))\n",
    "    b = np.exp(-np.sum((Y[:,np.newaxis,:]-Y[np.newaxis,:])**2,axis=-1))\n",
    "    a /= np.sum(a,axis=-1)[:,np.newaxis]\n",
    "    b /= np.sum(b,axis=-1)[:,np.newaxis]\n",
    "    result = 0\n",
    "    for i in range(len(X)):\n",
    "        for j in range(len(X)):\n",
    "            if i==j:\n",
    "                continue\n",
    "            result += a[i,j]*np.log(a[i,j]/b[i,j])\n",
    "    return result/len(X)\n",
    "S = np.array([neuron_to_brainstem_information[i]['soma_position'] for i in neuron_to_brainstem_information])\n",
    "result = {}\n",
    "for i,j in enumerate(target):\n",
    "    tmp = {}\n",
    "    for k,l in enumerate(neuron_to_brainstem_information):\n",
    "        if np.sum(info[l][i])>0:\n",
    "            if S[k][1]<3000:\n",
    "                m = 'dorsal'\n",
    "            else:\n",
    "                m = 'ventral'\n",
    "            if S[k][0]<6000:\n",
    "                m += '-anterior'\n",
    "            else:\n",
    "                m += '-posterior'\n",
    "            if m not in tmp:\n",
    "                tmp[m] = [[],[]]\n",
    "            tmp[m][0].append(S[k])\n",
    "            tmp[m][1].append(info[l][i])\n",
    "    for k in tmp:\n",
    "        tmp[k][0] = _norml(tmp[k][0])\n",
    "        tmp[k][1] = _norml(tmp[k][1])\n",
    "        #linearModel = LinearRegression()\n",
    "        #linearModel.fit(tmp[k][0],tmp[k][1])\n",
    "        #print(k,linearModel.score(tmp[k][0],tmp[k][1]))\n",
    "        if k not in result:\n",
    "            result[k] = []\n",
    "        result[k].append(KL(tmp[k][0],tmp[k][1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def _norml(x):\n",
    "    tmp = np.array(x)\n",
    "    return (tmp-np.mean(tmp,axis=0))/np.std(tmp)\n",
    "def KL(X,Y):\n",
    "    a = np.exp(-np.sum((X[:,np.newaxis,:]-X[np.newaxis,:])**2,axis=-1))\n",
    "    b = np.exp(-np.sum((Y[:,np.newaxis,:]-Y[np.newaxis,:])**2,axis=-1))\n",
    "    a /= np.sum(a,axis=-1)[:,np.newaxis]\n",
    "    b /= np.sum(b,axis=-1)[:,np.newaxis]\n",
    "    result = 0\n",
    "    for i in range(len(X)):\n",
    "        for j in range(len(X)):\n",
    "            if i==j:\n",
    "                continue\n",
    "            result += a[i,j]*np.log(a[i,j]/b[i,j])\n",
    "    return result/len(X)\n",
    "S = np.array([neuron_to_brainstem_information[i]['soma_position'] for i in neuron_to_brainstem_information])\n",
    "result = {}\n",
    "for i,j in enumerate(target):\n",
    "    tmp = {}\n",
    "    for k,l in enumerate(neuron_to_brainstem_information):\n",
    "        if np.sum(info[l][i])>0:\n",
    "            m = neuron_to_brainstem_information[l]['cortex_region']\n",
    "            if m not in tmp:\n",
    "                tmp[m] = [[],[]]\n",
    "            tmp[m][0].append(S[k])\n",
    "            tmp[m][1].append(info[l][i])\n",
    "    for k in tmp:\n",
    "        tmp[k][0] = _norml(tmp[k][0])\n",
    "        tmp[k][1] = _norml(tmp[k][1])\n",
    "        #linearModel = LinearRegression()\n",
    "        #linearModel.fit(tmp[k][0],tmp[k][1])\n",
    "        #print(k,linearModel.score(tmp[k][0],tmp[k][1]))\n",
    "        if k not in result:\n",
    "            result[k] = {}\n",
    "        result[k][j] = KL(tmp[k][0],tmp[k][1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sequences = [ 'ILA', 'PL', 'ORB', 'FRP','MOp','MOs', 'SSp', 'SSs', 'AUD', 'TEa',  'ECT', 'PERI',  'AI', 'GU', 'VISC','PTLp', 'VIS', 'ACA','RSP']\n",
    "X = np.zeros((len(sequences),len(target)))\n",
    "for ii,i in enumerate(sequences):\n",
    "    for jj,j in enumerate(target):\n",
    "        try:\n",
    "            X[ii,jj] = 1-result[i][j]\n",
    "        except:\n",
    "            pass\n",
    "fig = plt.figure(figsize=(3,5))\n",
    "plt.matshow(X.T,cmap='coolwarm')\n",
    "plt.xticks(range(len(sequences)),sequences)\n",
    "plt.yticks(range(len(target)),target)\n",
    "plt.title('Soma distribution of the PT neurons',fontsize=fontsize_title)\n",
    "plt.savefig(overall_path+'topology_index.pdf',format='pdf')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### arbor morphology index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = ['MB','P','MY']\n",
    "mask = 0\n",
    "for i in target:\n",
    "    mask += get_region_mask(i)\n",
    "info = {}\n",
    "for id,neuron in enumerate(neuron_to_brainstem_information):\n",
    "    info[neuron] = []\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            info[neuron].append(terminal.copy())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def generate_gaussian_kernel_3d(r,s):\n",
    "    x = np.linspace(-r, r, 2*r+1)\n",
    "    y = np.linspace(-r, r, 2*r+1)\n",
    "    z = np.linspace(-r, r, 2*r+1)\n",
    "    x, y, z = np.meshgrid(x, y,z)\n",
    "    tmp = np.exp(-(x**2 + y**2 + z**2) / (2*s**2))\n",
    "    return tmp/np.sum(tmp)\n",
    "plt.matshow(np.average(generate_gaussian_kernel_3d(r,12),axis=-1))\n",
    "plt.colorbar()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "kernel = generate_gaussian_kernel_3d(r,12)\n",
    "tmp = mask*0.0\n",
    "e = np.array(tmp.shape)\n",
    "r = 25\n",
    "info2 = {}\n",
    "for neuron in tool_bar(list(info.keys())):\n",
    "    tmp *= 0\n",
    "    if len(info[neuron])==0:\n",
    "        continue\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']\n",
    "    if region not in info2:\n",
    "        info2[region] = {'density_mean':[],'density_std':[],'volume':[]}\n",
    "    for i in info[neuron]:\n",
    "        a1 = (np.array(i)/10).astype(int)-r\n",
    "        a = np.where(a1<0,0,a1)\n",
    "        b1 = (np.array(i)/10).astype(int)+r+1\n",
    "        b = np.where(b1>e,e,b1)\n",
    "        c1 = a-a1\n",
    "        c2 = b1-b\n",
    "        tmp[a[0]:b[0],a[1]:b[1],a[2]:b[2]] += kernel[c1[0]:kernel.shape[0]-c2[0],c1[1]:kernel.shape[1]-c2[1],c1[2]:kernel.shape[2]-c2[2]]\n",
    "    tmp2 = tmp.flatten()\n",
    "    tmp2 = tmp2[tmp2>0]\n",
    "    info2[region]['density_mean'].append(np.mean(tmp2))\n",
    "    info2[region]['density_std'].append(np.std(tmp2))\n",
    "    info2[region]['volume'].append(np.sum(tmp>0)/len(info[neuron]))\n",
    "info3 = {}\n",
    "for neuron in tool_bar(list(info.keys())):\n",
    "    if len(info[neuron])==0:\n",
    "        continue\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']\n",
    "    if region not in info3:\n",
    "        info3[region] = []\n",
    "    info3[region].append(len(info[neuron]))\n",
    "tmp = {}\n",
    "for neuron in tool_bar(list(info.keys())):\n",
    "    if len(info[neuron])==0:\n",
    "        continue\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']\n",
    "    if region not in tmp:\n",
    "        tmp[region] = []\n",
    "    tmp[region].append(neuron)\n",
    "info4 = {}\n",
    "for region in tmp:\n",
    "    for j,k,l in zip(info3[region],info2[region]['volume'],tmp[region]):\n",
    "        info4[l] = [j*k*(1e-6),1-k/(2*r+1)**3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.save(overall_path+'_info.npy',info)\n",
    "np.save(overall_path+'_info2.npy',info2)\n",
    "np.save(overall_path+'_info3.npy',info3)\n",
    "np.save(overall_path+'_info4.npy',info4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = [[],[]]\n",
    "sequences = [ 'ILA', 'PL', 'ORB', 'FRP','MOp','MOs', 'SSp', 'SSs', 'AUD', 'TEa',  'ECT', 'PERI',  'AI', 'GU', 'VISC','PTLp', 'VIS', 'ACA','RSP']\n",
    "for i in sequences:\n",
    "    if i not in info2:\n",
    "        info2[i] = {'density_mean':[],'density_std':[],'volume':[]}\n",
    "medianprops = dict(linewidth=1.5,color='#A81E32')\n",
    "fig = plt.figure(figsize=(12,3))\n",
    "ax = fig.add_subplot(2,1,2)\n",
    "tmp = [1-np.array(info2[k]['volume'])/(2*r+1)**3 for k in sequences]\n",
    "tmp2 = [[np.median(i),j,i] for i,j in zip(tmp,sequences)]\n",
    "tmp2 = sorted(tmp2, key=lambda a: a[0])\n",
    "xticks = [i[1] for i in tmp2]\n",
    "data = [i[2] for i in tmp2]\n",
    "result[1] = [np.median(i) for i in data]\n",
    "ax.boxplot(data,medianprops=medianprops,widths=0.25)\n",
    "ax.set_xticks(range(1,len(xticks)+1),xticks,fontsize=fontsize_ticks)\n",
    "ax.set_xlabel('Modality',fontsize=fontsize_label)\n",
    "ax.set_ylabel('Density',rotation=90)\n",
    "ax = fig.add_subplot(2,1,1)\n",
    "data = [[np.array(i)*j*(1e-6) for i,j in zip(info2[k]['volume'],info3[k])] for k in xticks] #(10um**3)->1e-6(mm**3)\n",
    "result[0] = [np.median(i) for i in data]\n",
    "ax.boxplot(data,medianprops=medianprops,widths=0.25)\n",
    "ax.set_xticks([],[])\n",
    "ax.set_ylabel('Volume',rotation=90)\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(overall_path+'arbor_morphology_index.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.median(result[0])\n",
    "b = np.median(result[1])\n",
    "A = {'Vl_Dl':[],'Vh_Dl':[],'Vl_Dh':[],'Vh_Dh':[]}\n",
    "for i,j in enumerate(xticks):\n",
    "    m = ''\n",
    "    if result[0][i]>=a:\n",
    "        m = 'Vh'\n",
    "    else:\n",
    "        m = 'Vl'\n",
    "    if result[1][i]>=b:\n",
    "        m += '_Dh'\n",
    "    else:\n",
    "        m += '_Dl'\n",
    "    A[m].append(j)\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp = {'Vl_Dl': [],'Vh_Dl': [],'Vl_Dh': [],'Vh_Dh': []}\n",
    "for i in info4:\n",
    "    m = ''\n",
    "    if info4[i][0]>=a:\n",
    "        m = 'Vh'\n",
    "    else:\n",
    "        m = 'Vl'\n",
    "    if info4[i][1]>=b:\n",
    "        m += '_Dh'\n",
    "    else:\n",
    "        m += '_Dl'\n",
    "    tmp[m].append(i)\n",
    "for i,j in enumerate(tmp):\n",
    "    save_scene(tmp[j],color_pool[i],overall_path+j)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp = {}\n",
    "for i in info4:\n",
    "    module = cortical_module_transform[neuron_to_brainstem_information[i]['cortex_region']]\n",
    "    if module not in tmp:\n",
    "        tmp[module] = []\n",
    "    tmp[module].append(info4[i])\n",
    "for i in tmp:\n",
    "    X = np.array(tmp[i])\n",
    "    plt.scatter(X[:,0],X[:,1],s=0.75,color=cortical_module_color[i],label=i)\n",
    "plt.xlabel('Volume',fontsize=fontsize_label)\n",
    "plt.ylabel('Density',rotation=90)\n",
    "plt.legend()\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(overall_path+'arbor_morphology_index_plot_module.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### examples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i,j in zip(['221481548.swc','221423045.swc'],[1,5]):\n",
    "    neuronvis = nv.neuronVis(size=(1200,900))\n",
    "    neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "    set_view(neuronvis,'anterior')\n",
    "    neuron_tree = iondata.getNeuronTreeByID(i[:-7],i[-7:])\n",
    "    neuron_tree.somaRadius = 90\n",
    "    neuronvis.addNeuronTree(neuron_tree,axonColor=color_pool[j],somaColor=[0.5,0.5,0.5],dendriteColor=[0.5,0.5,0.5],mirrorToRight=True,isLine=True)\n",
    "    for k in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "        set_view(neuronvis,k)\n",
    "        neuronvis.render.savepng(overall_path+f'topology_example_{i[:-4]}_{k}.png')\n",
    "    neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i,j in zip(['221481548.swc','221423045.swc'],[1,5]):\n",
    "    neuronvis = nv.neuronVis(size=(1200,900))\n",
    "    neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "    neuronvis.clear(root=True)\n",
    "    set_view(neuronvis,'anterior')\n",
    "    neuron_tree = iondata.getNeuronTreeByID(i[:-7],i[-7:])\n",
    "    neuron_tree.somaRadius = 90\n",
    "    neuronvis.addNeuronTree(neuron_tree,axonColor=color_pool[j],somaColor=[0.5,0.5,0.5],dendriteColor=[0.5,0.5,0.5],mirrorToRight=True,isLine=False)\n",
    "    for k in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "        set_view(neuronvis,k)\n",
    "        neuronvis.render.savepng(overall_path+f'topology_example_{i[:-4]}_{k}.png')\n",
    "    neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i,j in zip(['221423045.swc'],[5]):\n",
    "    neuronvis = nv.neuronVis(size=(1200,900))\n",
    "    neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "    set_view(neuronvis,'anterior')\n",
    "    neuron_tree = iondata.getNeuronTreeByID(i[:-7],i[-7:])\n",
    "    neuron_tree.somaRadius = 90\n",
    "    neuronvis.addNeuronTree(neuron_tree,axonColor=color_pool[j],somaColor=[0.5,0.5,0.5],dendriteColor=[0.5,0.5,0.5],mirrorToRight=True,isLine=True)\n",
    "    for k in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "        set_view(neuronvis,k)\n",
    "        neuronvis.render.savepng(overall_path+f'topology_example_{i[:-4]}_{k}.png')\n",
    "    neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "S = {'Vh_Dh':'221514002.swc','Vh_Dl':'220198264.swc','Vl_Dh':'221423123.swc','Vl_Dl':'221528015.swc'}\n",
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "for l,j in zip(S,[0,1,5,3]):\n",
    "    i = S[l]\n",
    "    neuronvis.clear()\n",
    "    neuron_tree = iondata.getNeuronTreeByID(i[:-7],i[-7:])\n",
    "    neuron_tree.somaRadius = 90\n",
    "    neuronvis.addNeuronTree(neuron_tree,axonColor=color_pool[j],somaColor=[0.5,0.5,0.5],dendriteColor=[0.5,0.5,0.5],mirrorToRight=True,isLine=False)\n",
    "    for k in ['right']:\n",
    "        set_view(neuronvis,k)\n",
    "        neuronvis.render.savepng(overall_path+f'topology_example_{i}_{k}.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "S = {'Vh_Dh':'221514002.swc','Vh_Dl':'221224167.swc','Vl_Dh':'221423123.swc','Vl_Dl':'221528015.swc'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in S:\n",
    "    print(S[i],neuron_to_brainstem_information[S[i]]['cortex_region']+neuron_to_brainstem_information[S[i]]['cortex_subregion'],i,info4[S[i]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## bilateral relationship"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = ['MB','P','MY']\n",
    "info = {}\n",
    "for i in target:\n",
    "    mask = get_region_mask(i)\n",
    "    for id,neuron in enumerate(neuron_to_brainstem_information):\n",
    "        if neuron not in info:\n",
    "            info[neuron] = []\n",
    "        tmp = [0,0]\n",
    "        a = neuron_to_brainstem_information[neuron]['soma_position'][2]<5700\n",
    "        for terminal in neuron_terminals[neuron]['terminals']:\n",
    "            if if_inside_mask(mask,np.array(terminal)/10):\n",
    "                b = terminal[2]<5700\n",
    "                if a==b:\n",
    "                    tmp[0] += 1\n",
    "                else:\n",
    "                    tmp[1] += 1\n",
    "        info[neuron].append(tmp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "S = np.array([neuron_to_brainstem_information[i]['soma_position'] for i in neuron_to_brainstem_information])\n",
    "for i,j in enumerate(target):\n",
    "    tmp = {}\n",
    "    for k,l in enumerate(neuron_to_brainstem_information):\n",
    "        if np.sum(info[l][i])>0:\n",
    "            if S[k][1]<3000:\n",
    "                m = 'dorsal'\n",
    "            else:\n",
    "                m = 'ventral'\n",
    "            if S[k][0]<6000:\n",
    "                m += '-anterior'\n",
    "            else:\n",
    "                m += '-posterior'\n",
    "            if m not in tmp:\n",
    "                tmp[m] = [[],[]]\n",
    "            tmp[m][0].append(S[k])\n",
    "            tmp[m][1].append(info[l][i][0]/np.sum(info[l][i]))\n",
    "    for k in tmp:\n",
    "        print(k,np.mean(tmp[k][1],axis=0))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## branches type"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mode = 1\n",
    "angle_threshold = 45\n",
    "length_ratio_threshold = 0.8\n",
    "length_threshold = -1\n",
    "return_nonshaft = False\n",
    "mirror = True\n",
    "final_lines = []\n",
    "final_colors = []\n",
    "final_lines2 = []\n",
    "final_colors2 = []\n",
    "for i in cortex_neuron_name:\n",
    "    control_console_output(0)\n",
    "    neuron_tree = iondata.getNeuronTreeByID(i[:-7],i[-7:])\n",
    "    control_console_output(1)\n",
    "    [[lines,lines_color],[lines2,lines_color2]] = get_shaft(neuron_tree,shaft_color=color_pool[0].tolist(),mode=mode,return_nonshaft=return_nonshaft,angle_threshold=45,length_ratio_threshold=length_ratio_threshold,length_threshold=length_threshold,mirror=mirror)\n",
    "    final_lines += lines\n",
    "    final_colors += lines_color\n",
    "    final_lines2 += lines2\n",
    "    final_colors2 += lines_color2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear()\n",
    "draw_lines(neuronvis,final_lines,final_colors)\n",
    "neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#mask = (get_region_mask('MB')+get_region_mask('P')+get_region_mask('MY'))>0\n",
    "import colorsys\n",
    "def cartesian_to_polar_numpy(x):\n",
    "    r = np.hypot(x[0],x[1])\n",
    "    theta = np.arctan2(x[1],x[0])\n",
    "    return r, theta\n",
    "A = [[],[]]\n",
    "for i,j in zip(final_lines,final_colors):\n",
    "    if len(i)<2:\n",
    "        continue\n",
    "    if if_inside_mask(mask,np.array(i[-1])/10):\n",
    "        A[0].append(i)\n",
    "        #A[1].append(j)\n",
    "        c = []\n",
    "        for a,b in zip(i[:-1],i[1:]):\n",
    "            r,theta = cartesian_to_polar_numpy(b-a)\n",
    "            c.append(colorsys.hsv_to_rgb((theta/np.pi*180+180)/360,0.9,0.9))\n",
    "        c = [c[0]]+c\n",
    "        A[1].append(c)\n",
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear()\n",
    "draw_lines(neuronvis,A[0],A[1])\n",
    "neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A = []\n",
    "for theta in [0,90,180,270,360]:\n",
    "    print(theta,colorsys.hsv_to_rgb(theta/360,0.9,0.9))\n",
    "    A.append([colorsys.hsv_to_rgb(theta/360,0.9,0.9)])\n",
    "plt.matshow(np.array(A).transpose(1,0,2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from matplotlib.colors import hsv_to_rgb\n",
    "\n",
    "# 创建极坐标系\n",
    "fig = plt.figure(figsize=(6, 6))\n",
    "ax = fig.add_subplot(111, projection='polar')\n",
    "\n",
    "# 生成角度和半径网格\n",
    "theta = np.linspace(0, 2*np.pi, 360)  # 0~360度，精度1度\n",
    "r = np.linspace(0, 1, 100)            # 半径范围（0中心~1边缘）\n",
    "T, R = np.meshgrid(theta, r)          # 生成网格\n",
    "\n",
    "# 计算HSV颜色（H=角度，S=1，V=1）\n",
    "H = T / (2 * np.pi)                  # 归一化到 [0,1)\n",
    "S = np.ones_like(R)                  # 饱和度固定为1\n",
    "V = np.ones_like(R)                  # 明度固定为1\n",
    "\n",
    "# 转换为RGB颜色矩阵（形状需与网格一致）\n",
    "HSV = np.stack([H, S, V], axis=-1)   # 拼接HSV数组\n",
    "RGB = hsv_to_rgb(HSV)                # 转换为RGB\n",
    "\n",
    "# 绘制色环（隐藏极坐标轴线）\n",
    "ax.pcolormesh(T, R, RGB, shading='auto')\n",
    "ax.set_xticks([])                    # 隐藏角度刻度\n",
    "ax.set_yticks([])                    # 隐藏半径刻度\n",
    "ax.spines['polar'].set_visible(False) # 隐藏极坐标边框\n",
    "\n",
    "plt.title('HSV Color Wheel', pad=20)\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(overall_path+'direction.pdf',format='pdf')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear()\n",
    "draw_lines(neuronvis,A[0],A[1])\n",
    "neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#mask = (get_region_mask('MB')+get_region_mask('P')+get_region_mask('MY'))>0\n",
    "tmp1 = np.stack([mask[...,0]*0,mask[...,0]*0,mask[...,0]*0],axis=-1).astype(float)\n",
    "tmp2 = tmp1*0\n",
    "for line,color in zip(A[0],A[1]):\n",
    "    for a,b in zip(line,color):\n",
    "        try:\n",
    "            tmp1[int(a[0]/10),int(a[1]/10),:] += np.array(b)\n",
    "            tmp2[int(a[0]/10),int(a[1]/10),:] += 1\n",
    "        except:\n",
    "            pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure(figsize=(12.3*3,8*3))\n",
    "a = (tmp1/np.where(tmp2==0,1,tmp2)).transpose(1,0,2)*1\n",
    "mask = np.sum(a,axis=-1,keepdims=True)==0\n",
    "a = a*(1-mask)+mask*np.array([[1,1,1]])\n",
    "plt.matshow(a)\n",
    "plt.axis('off')\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(overall_path+'path.pdf',format='pdf')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from PIL import Image\n",
    "from numpy import asarray\n",
    "\n",
    "# Load the image\n",
    "image = Image.open(overall_path+'tmp.png')\n",
    "\n",
    "# Convert the image to a NumPy array\n",
    "numpy_data = asarray(image)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from scipy.ndimage import uniform_filter\n",
    "\n",
    "def compute_local_variance(matrix, window_size=3):\n",
    "    \"\"\"计算局部方差矩阵\"\"\"\n",
    "    matrix = matrix.astype(np.float64)\n",
    "    # 计算均值和均方\n",
    "    mean = uniform_filter(matrix, window_size, mode='reflect')\n",
    "    mean_sq = uniform_filter(matrix**2, window_size, mode='reflect')\n",
    "    # 方差公式：Var = E[X²] - (E[X])²\n",
    "    variance = mean_sq - mean**2\n",
    "    return variance\n",
    "\n",
    "# 示例：生成模拟数据并计算\n",
    "np.random.seed(0)\n",
    "data = numpy_data\n",
    "\n",
    "# 计算局部方差（窗口大小可调）\n",
    "window_size = 5\n",
    "local_var = compute_local_variance(data, window_size)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.matshow(np.sum(local_var[...,:3],axis=-1)/2000)\n",
    "plt.colorbar()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Motor system"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "motor_system_path = data_path+'motor_system/'\n",
    "create_dir(motor_system_path)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## region visualization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "RF = ['MRN','CUN','PRNr','PRNc','GRN','IRN','PARN','MARN','PGRNd','MDRNd','MDRNv','LRNm','LRNp','LIN']\n",
    "MN_midline = ['INC','ND','MA3','EW','III','Su3','IV','DTN','PDTg','SG','VI','PRP','DMX','XII']\n",
    "MN_lateral = ['RN','PPN','V','SUT','P5','VII','AMB','PGRNl']\n",
    "MN = MN_midline+MN_lateral\n",
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "neuronvis.addRegion('MB')\n",
    "neuronvis.addRegion('P')\n",
    "neuronvis.addRegion('MY')\n",
    "for i in MN_midline:\n",
    "    neuronvis.addRegion(i,color=color_pool[0])\n",
    "for i in MN_lateral:\n",
    "    neuronvis.addRegion(i,color=color_pool[3])\n",
    "for i in ['left','dorsal']:\n",
    "    set_view(neuronvis,i)\n",
    "    if i=='dorsal':\n",
    "        neuronvis.render.setClip(False)\n",
    "    else:\n",
    "        neuronvis.render.setClip(True)\n",
    "    neuronvis.render.savepng(motor_system_path+'region_MN_'+i+'.png')\n",
    "neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "neuronvis.addRegion('MB')\n",
    "neuronvis.addRegion('P')\n",
    "neuronvis.addRegion('MY')\n",
    "for i in RF:\n",
    "    neuronvis.addRegion(i,color=color_pool[5])\n",
    "for i in ['dorsal','left']:\n",
    "    set_view(neuronvis,i)\n",
    "    if i=='dorsal':\n",
    "        neuronvis.render.setClip(False)\n",
    "    else:\n",
    "        neuronvis.render.setClip(True)\n",
    "    neuronvis.render.savepng(motor_system_path+'region_RF_'+i+'.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "neuronvis.addRegion('MB')\n",
    "neuronvis.addRegion('P')\n",
    "neuronvis.addRegion('MY')\n",
    "for i in MN_midline:\n",
    "    neuronvis.addRegion(i,color=color_pool[1])\n",
    "for i in MN_lateral:\n",
    "    neuronvis.addRegion(i,color=color_pool[0])\n",
    "for i in RF:\n",
    "    neuronvis.addRegion(i,color=color_pool[5])\n",
    "for i in ['dorsal','left']:\n",
    "    set_view(neuronvis,i)\n",
    "    if i=='dorsal':\n",
    "        neuronvis.render.setClip(False)\n",
    "    else:\n",
    "        neuronvis.render.setClip(True)\n",
    "    neuronvis.render.savepng(motor_system_path+'region_RF_MN_'+i+'.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## neural examples for segmentation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i,j in zip(['211181003.swc','221085134.swc'],[0,0]):\n",
    "    neuronvis = nv.neuronVis(size=(1200,900))\n",
    "    neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "    neuronvis.clear(root=False)\n",
    "    neuron_tree = iondata.getNeuronTreeByID(i[:-7],i[-7:])\n",
    "    neuron_tree.somaRadius = 100\n",
    "    neuronvis.addNeuronTree(neuron_tree,axonColor=color_pool[j].tolist(),somaColor=[0.5,0.5,0.5],dendriteColor=[0.5,0.5,0.5],mirrorToRight=True,isLine=False)\n",
    "    for k in ['anterior','dorsal','left','right']:\n",
    "        set_view(neuronvis,k)\n",
    "        neuronvis.render.savepng(motor_system_path+'neural_examples_'+i+'_'+k+'.png')\n",
    "    neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Compare"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = RF+MN\n",
    "compare_info = [get_axonal_statics_by_regions(i,target,'terminalregion') for i in cortex_neuron_name]\n",
    "X = {}\n",
    "for i,j in zip(cortex_neuron_name,compare_info):\n",
    "    region = neuron_to_brainstem_information[i]['cortex_region']\n",
    "    if region not in X:\n",
    "        X[region] = []\n",
    "    X[region].append(j)\n",
    "tmp = {}\n",
    "for i in cortical_module:\n",
    "    tmp[i] = []\n",
    "    for j in cortical_module[i]:\n",
    "        tmp[i] += X[j]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = []\n",
    "for i in tmp:\n",
    "    a += tmp[i]\n",
    "a = np.mean(a,axis=0)\n",
    "print(np.sum(a[:len(RF)]),np.sum(a[len(RF):]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_labels = ['VIS','SS','AUD','MO','Cingulate','Insula','TEa','PFC']\n",
    "colors = [1,1,1,0,0,0,0]\n",
    "width = 0.15\n",
    "step = 0.2\n",
    "step2 = 2\n",
    "fig = plt.figure(figsize=(6,4))\n",
    "ax = fig.add_subplot(1,1,1)\n",
    "y = []\n",
    "for i,j in enumerate(x_labels):\n",
    "    data = np.mean(tmp[j],axis=0)\n",
    "    data = np.array([np.sum(data[:len(RF)]),np.sum(data[len(RF):-len(MN_lateral)]),np.sum(data[-len(MN_lateral):])]).astype(float)\n",
    "    y.append(data)\n",
    "y = np.array(y)\n",
    "ax.bar(range(len(y)),y[:,0],color=color_pool[5],label='RF',width=0.25)\n",
    "ax.bar(range(len(y)),y[:,1],bottom=y[:,0],color=color_pool[0],label='MN_midline',width=0.25)\n",
    "ax.bar(range(len(y)),y[:,2],bottom=y[:,0]+y[:,1],color=color_pool[3],label='MN_lateral',width=0.25)\n",
    "ax.set_xticks(range(len(x_labels)),x_labels)\n",
    "ax.set_xlabel('Targets',fontsize=fontsize_label)\n",
    "ax.set_ylabel('Average terminal number',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "ax.set_title('Reticular vs motor nuclei',fontsize=fontsize_title)\n",
    "plt.legend()\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(motor_system_path+'RF_vs_MN.pdf',format='pdf')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.sum(data[-len(MN_lateral):])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def func(pct, allvals):\n",
    "    absolute = int(np.round(pct/100.*np.sum(allvals)))\n",
    "    return f\"{pct:.1f}%\\n({absolute:d})\"\n",
    "data = np.mean(tmp['MO'],axis=0)\n",
    "plt.pie(data[-len(MN_lateral):],colors=color_pool[:len(MN_lateral)],labels=MN_lateral, autopct=lambda pct: func(pct,data[-len(MN_lateral):]),textprops=dict(color=\"w\"))\n",
    "plt.legend()\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(motor_system_path+'MO_MN_distribution.pdf',format='pdf')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def func(pct, allvals):\n",
    "    absolute = int(np.round(pct/100.*np.sum(allvals)))\n",
    "    return f\"{pct:.1f}%\\n({absolute:d})\"\n",
    "data = []\n",
    "for i in tmp:\n",
    "    data += tmp[i]\n",
    "data = np.mean(data,axis=0)\n",
    "plt.pie(data[-len(MN_lateral):],colors=color_pool[:len(MN_lateral)],labels=MN_lateral, autopct=lambda pct: func(pct,data[-len(MN_lateral):]),textprops=dict(color=\"w\"))\n",
    "plt.legend()\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(motor_system_path+'MN_distribution.pdf',format='pdf')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Level segments"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mask_2D,header = nrrd.read(motor_system_path+'MAX_RF_grey41.nrrd')\n",
    "for i,j in enumerate([111,130,150,170,190,210,230,220,255]):\n",
    "    mask_2D = np.where(mask_2D==j,i+1,mask_2D)\n",
    "mask_2D = np.where(np.sum(get_region_mask('CUN'),axis=-1)>0,4,mask_2D)\n",
    "nrrd.write(motor_system_path+'RF_Segment_Mask.nrrd',mask_2D)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.matshow(mask_2D.T)\n",
    "plt.axis('off')\n",
    "plt.title(f'Brainstem segments',fontsize=fontsize_title)\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(motor_system_path+'Brainstem_segment.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mask,header = nrrd.read(motor_system_path+'RF.nrrd') \n",
    "mask = np.where(get_region_mask('PGRNl'),0,mask)\n",
    "mask = np.where(get_region_mask('CUN'),1,mask)\n",
    "info = {}\n",
    "for i in neuron_to_brainstem_information:\n",
    "    d = neuron_to_brainstem_information[i]['soma_depth']\n",
    "    p = neuron_to_brainstem_information[i]['soma_flatten_position']\n",
    "    info[i] = [d,p,np.zeros((2,9))]\n",
    "    for terminal in neuron_terminals[i]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            r = int(mask_2D[int(terminal[0]/10),int(terminal[1]/10)])-1\n",
    "            a = neuron_terminals[i]['mirror_mark']\n",
    "            b = terminal[2]>5700\n",
    "            if a==b:\n",
    "                info[i][2][0,r] += 1\n",
    "            else:\n",
    "                info[i][2][1,r] += 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### bilateral"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = np.zeros((2,np.max(mask_2D)))\n",
    "for i in info:\n",
    "    for j in range(np.max(mask_2D)):\n",
    "        X[0,j] += info[i][2][0,j]\n",
    "        X[1,j] += info[i][2][1,j]\n",
    "fig = plt.figure(figsize=(np.max(mask_2D),3))\n",
    "ax = fig.add_subplot(2,1,1)\n",
    "ax.bar(range(np.max(mask_2D)),np.sum(X,axis=0),width=0.25,color=color_pool[3])\n",
    "ax.set_xticks([],[])\n",
    "ax.set_ylabel('Number',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "ax.set_title('Distribution',fontsize=fontsize_title)\n",
    "ax = fig.add_subplot(2,1,2)\n",
    "ax.matshow(X/np.array([np.sum(X,axis=0)]),cmap='Purples')\n",
    "ax.set_xticks(range(np.max(mask_2D)),range(1,np.max(mask_2D)+1))\n",
    "ax.set_yticks([0,1],['ipsi','contra'])\n",
    "ax.set_xlabel('Reticular formation segment',fontsize=fontsize_label)\n",
    "ax.tick_params(axis='x', which='both', top=False, bottom=True, labelbottom=True)\n",
    "ax.set_ylabel('Side',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "ax.set_title('Reticular vs motor nuclei',fontsize=fontsize_title)\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(motor_system_path+'RF_distribution_terminal.pdf',format='pdf')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### origin"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for hg in range(1,np.max(mask_2D)+1):\n",
    "    r = 200\n",
    "    s = 125\n",
    "    transition_color = np.array([[255,255,255],[161,238,204],[255,242,193],[246,49,141]])/255.0\n",
    "    cmap1 = mcolors.LinearSegmentedColormap.from_list('cmap1', transition_color)\n",
    "    kernel = generate_gaussian_kernel(r,s)\n",
    "    tmp = _flatmap[...,0]*0.0\n",
    "    soma_list = [[],[]]\n",
    "    for i in info:\n",
    "        d,p = info[i][:2]\n",
    "        if p==[]:\n",
    "            continue\n",
    "        e = np.sum(info[i][2][:,hg-1])\n",
    "        fill_kernel(tmp,np.array(p[::-1])*2,e/neuron_to_brainstem_information[i]['relative_density'],r,kernel)\n",
    "        soma_list[0].append(np.array(p[::-1])*2)\n",
    "        soma_list[1].append(e)\n",
    "    soma_list[1] = np.array(soma_list[1])**0.5\n",
    "    soma_list[1] /= np.max(soma_list[1])\n",
    "    #result = plot_transition_color(transition_color,tmp)\n",
    "    result = np.where(_flatmap[...,0]==-1,0,tmp)\n",
    "    #for k,l in zip(soma_list[0],soma_list[1]):\n",
    "        #cv.circle(result,[int(np.round(k[1])), int(np.round(k[0]))],int(np.round(l*10))+1,np.array([0.5,0.5,0.5]),-1)\n",
    "    fig = plt.figure(figsize=(7,5))\n",
    "    result /= np.max(result)\n",
    "    plt.imshow(result,cmap=cmap1,vmin=0,vmax=1)\n",
    "    plt.colorbar(extend='both')\n",
    "    plt.imshow(flatmap_mask,zorder=1)\n",
    "    plt.imshow(flatmap2,zorder=1)\n",
    "    plt.axis('off')\n",
    "    plt.xlim(flatten_map_r[0],flatten_map_r[1])\n",
    "    plt.ylim(flatten_map_r[2],flatten_map_r[3])\n",
    "    plt.title(f'Brainstem segment {hg}',fontsize=fontsize_title)\n",
    "    plt.rcParams['pdf.fonttype'] = 42\n",
    "    plt.rcParams['ps.fonttype'] = 42\n",
    "    plt.savefig(motor_system_path+f'Brainstem_segment_{hg}.pdf',format='pdf',bbox_inches='tight')\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### come from different neurons"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = flatmap2[...,:3]*1\n",
    "attention_neuron = []\n",
    "n = 0\n",
    "for i in neuron_to_brainstem_information:\n",
    "    d = neuron_to_brainstem_information[i]['soma_depth']\n",
    "    p = neuron_to_brainstem_information[i]['soma_flatten_position']\n",
    "    if len(p)==0:\n",
    "        continue\n",
    "    if np.linalg.norm(np.array([2000,1050])-np.array(p)*2)<175:\n",
    "        n += 1\n",
    "        cv.circle(a,[int(np.round(p[0]))*2, int(np.round(p[1]))*2],10,np.array([1.0,0.25,0.25]),-1)\n",
    "        #print([int(np.round(p[1])), int(np.round(p[0]))])\n",
    "        attention_neuron.append(i)\n",
    "plt.imshow(a)\n",
    "plt.show()\n",
    "print(len(attention_neuron))\n",
    "tmp = []\n",
    "for i in attention_neuron:\n",
    "    tmp.append(np.sum(info[i][2],axis=0))\n",
    "tmp = np.array(tmp)\n",
    "group = {i:[[],[]] for i in range(9)}\n",
    "for i,j in zip(attention_neuron,tmp):\n",
    "    group[np.argmax(j)][0].append(i)\n",
    "    group[np.argmax(j)][1].append(j)\n",
    "    if np.sum(j)!=0:\n",
    "        group[np.argmax(j)][1][-1] /= np.sum(j)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp2 = []\n",
    "for i in range(9):\n",
    "    tmp2 += group[i][1]\n",
    "tmp2 = np.array(tmp2)\n",
    "plt.matshow(tmp2.T,cmap='Purples',vmin=0,vmax=1)\n",
    "plt.colorbar()\n",
    "plt.xlabel(f'Neurons (n=151)',fontsize=fontsize_label)\n",
    "plt.ylabel(f'Segments',fontsize=fontsize_label)\n",
    "plt.title(f'Frontal eye field in reticular segments',fontsize=fontsize_title)\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(motor_system_path+f'FEF_in_RF.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(9):\n",
    "    save_scene(group[i][0],color_pool[i],motor_system_path+f'FEF_segment{i+1}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "t = {}\n",
    "for i in range(9):\n",
    "    t[i] = [info[j][0] for j in group[i][0]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = [-np.mean(i)*20 for i in list(t.values())]\n",
    "plt.plot(x,linewidth=1,color='k',marker='.')\n",
    "y = [np.mean(x[:4]),np.mean(x[4:6]),np.mean(x[6:])]\n",
    "plt.plot([1.5,4.5,7],y,linewidth=1,color='r',linestyle='--',marker='o')\n",
    "plt.xlabel(f'Segments',fontsize=fontsize_label)\n",
    "plt.ylabel(f'Depth',fontsize=fontsize_label)\n",
    "plt.title(f'Frontal eye field in reticular segments depth',fontsize=fontsize_title)\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(motor_system_path+f'FEF_in_RF_depth.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### segment arrangement"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mask,header = nrrd.read(motor_system_path+'RF.nrrd') "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "attention_x = {}\n",
    "for neuron in attention_neuron:\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            g = mask_2D[int(terminal[0]/10),int(terminal[1]/10)]\n",
    "            if g==0:\n",
    "                continue\n",
    "            g = g-1\n",
    "            if g not in attention_x:\n",
    "                attention_x[g] = []\n",
    "            a = neuron_terminals[neuron]['mirror_mark']\n",
    "            attention_x[g].append(terminal.copy())\n",
    "            if a:\n",
    "                attention_x[g][-1][2] = 11400-attention_x[g][-1][2]\n",
    "x = {}\n",
    "for neuron in neuron_to_brainstem_information:\n",
    "    if neuron in attention_neuron:\n",
    "        continue\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']\n",
    "    if region not in x:\n",
    "        x[region] = {}\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            g = mask_2D[int(terminal[0]/10),int(terminal[1]/10)]\n",
    "            if g==0:\n",
    "                continue\n",
    "            g = g-1\n",
    "            if g not in x[region]:\n",
    "                x[region][g] = []\n",
    "            a = neuron_terminals[neuron]['mirror_mark']\n",
    "            x[region][g].append(terminal.copy())\n",
    "            if a:\n",
    "                x[region][g][-1][2] = 11400-x[region][g][-1][2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_contour(X):\n",
    "    X = np.array(X).astype(float)\n",
    "    filter = torch.ones(1,1,3,3)\n",
    "    tmp = torch.conv2d(torch.tensor(X,dtype=torch.float32).unsqueeze(0).unsqueeze(0),filter,padding='same').numpy().reshape(X.shape[0],X.shape[1])\n",
    "    return np.where(tmp==X*9,0.0,1.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from scipy.stats import ks_2samp\n",
    "_config = {'region':{1:['SSp'],2:['MOp'],3:['TEa','ECT'],4:['TEa','ECT'],5:[],6:[],7:['AI'],8:['AI'],9:['MOs']},'color':{'SSp':1,'MOp':3,'TEa':10,'ECT':10,'AI':5,'MOs':7}}\n",
    "fig,ax = plt.subplots(3,2,figsize=(20,12))\n",
    "for i in range(6):\n",
    "    f = []\n",
    "    if i==5:\n",
    "        ax[i//2,i%2].imshow(get_contour(np.sum(get_region_mask('PRNc'),axis=0)>0),cmap='RdGy',vmin=-1,vmax=1)\n",
    "    else:\n",
    "        ax[i//2,i%2].imshow(get_contour(np.sum(np.array([mask_2D==(i+1)]).transpose(1,2,0)*mask,axis=0)>0),cmap='RdGy',vmin=-1,vmax=1)\n",
    "    try:\n",
    "        tmp = np.array(attention_x[i])/10\n",
    "        ax[i//2,i%2].scatter(tmp[:,2],tmp[:,1],color=color_pool[0],s=0.05)\n",
    "        a,b = estimate_density_function(tmp[:,2],parameter=[0,1140,1,11],weight=[])\n",
    "        ax[i//2,i%2].plot(range(1140),-b*5000+280,color=color_pool[0])\n",
    "        f = tmp[:,2]\n",
    "    except:\n",
    "        pass\n",
    "    q = []\n",
    "    c = 0\n",
    "    for j in _config['region'][i+1]:\n",
    "        try:\n",
    "            tmp = np.array(x[j][i])/10\n",
    "            q += tmp[:,2].tolist()\n",
    "            ax[i//2,i%2].scatter(tmp[:,2],tmp[:,1],color=color_pool[_config['color'][j]],s=0.05)\n",
    "            c = color_pool[_config['color'][j]]\n",
    "        except:\n",
    "            pass\n",
    "    if q!=[]:\n",
    "        a,b = estimate_density_function(q,parameter=[0,1140,1,11],weight=[])\n",
    "        ax[i//2,i%2].plot(range(1140),-b*5000+280,color=c)\n",
    "        q = np.round(stats.mannwhitneyu(f,q)[1],4)\n",
    "    ax[i//2,i%2].set_xlim(350,790)\n",
    "    ax[i//2,i%2].set_ylim(700,150)\n",
    "    ax[i//2,i%2].set_xticks([],[])\n",
    "    ax[i//2,i%2].set_yticks([],[])\n",
    "    ax[i//2,i%2].set_title(f'Segment {i+1}, (p={q})')\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(motor_system_path+f'FEF_arrangement.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### example neurons"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for index,i in enumerate(['221224268.swc','221225197.swc','221225218.swc','221224142.swc','221224197.swc','230107045.swc']):\n",
    "    neuronvis = nv.neuronVis(size=(1200,900))\n",
    "    neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "    neuronvis.clear(root=False)\n",
    "    neuron_tree = iondata.getNeuronTreeByID(i[:-7],i[-7:])\n",
    "    neuron_tree.somaRadius = 100\n",
    "    neuronvis.addNeuronTree(neuron_tree,axonColor=color_pool[index],somaColor=[0.5,0.5,0.5],dendriteColor=[0.5,0.5,0.5],mirrorToRight=True,isLine=False)\n",
    "    set_view(neuronvis,'right')\n",
    "    neuronvis.addRegion('PRNr',[0,0,0])\n",
    "    neuronvis.addRegion('MRN',[0,0,0])\n",
    "    neuronvis.addRegion('PARN',[0,0,0])\n",
    "    neuronvis.addRegion('MDRNv',[0,0,0])\n",
    "    neuronvis.addRegion('MDRNd',[0,0,0])\n",
    "    neuronvis.render.savepng(motor_system_path+f'segment_{index+1}_neuron_examples_'+i+'.png')\n",
    "    neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "root_mask_2D = np.sum(get_region_mask('root'),axis=-1)>0\n",
    "for index in range(9):\n",
    "    for neuron in Scene.scene2List(motor_system_path+f'FEF_segment{index+1}.nv'):\n",
    "        i = neuron['sampleid']+neuron['name']\n",
    "        plt.matshow(get_contour(mask_2D.T)+get_contour(root_mask_2D.T),cmap='RdGy',vmin=-1,vmax=1)\n",
    "        neuron_tree = iondata.getNeuronTreeByID(i[:-7],i[-7:])\n",
    "        for edge in neuron_tree.edges:\n",
    "            tmp = []\n",
    "            for point in edge.data:\n",
    "                tmp.append(point.xyz)\n",
    "            tmp = np.array(tmp)/10\n",
    "            color = color_pool[index] if edge.data[0].type!=3 else [0.5,0.5,0.5]\n",
    "            plt.plot(tmp[:,0],tmp[:,1],color=color,linewidth=0.5)\n",
    "        plt.scatter(neuron_tree.root.x/10,neuron_tree.root.y/10,s=5,color=[0.25,0.25,0.25])\n",
    "        plt.axis('off')\n",
    "        plt.rcParams['pdf.fonttype'] = 42\n",
    "        plt.rcParams['ps.fonttype'] = 42\n",
    "        plt.savefig(motor_system_path+f'FEF_arrangement_example_{i}.pdf',format='pdf',bbox_inches='tight')\n",
    "        plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=False)\n",
    "for i,j in zip (['221225173.swc','220187006.swc'],[0,3]):\n",
    "    neuron_tree = iondata.getNeuronTreeByID(i[:-7],i[-7:])\n",
    "    neuron_tree.somaRadius = 100\n",
    "    neuronvis.addNeuronTree(neuron_tree,axonColor=color_pool[j],somaColor=[0.5,0.5,0.5],dendriteColor=[0.5,0.5,0.5],mirrorToRight=True,isLine=False)\n",
    "neuronvis.addRegion('MRN')\n",
    "set_view(neuronvis,'anterior')\n",
    "neuronvis.render.savepng(motor_system_path+f'segment_2_integration.png')\n",
    "neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=False)\n",
    "for i in ['MRN','PRNr','PRNc','MDRNv','MDRNd']:\n",
    "    neuronvis.addRegion(i)\n",
    "set_view(neuronvis,'anterior')\n",
    "neuronvis.render.savepng(motor_system_path+f'RF.png')\n",
    "neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=False)\n",
    "for i,j in zip (['221225178.swc','221530126.swc'],[0,10]):\n",
    "    neuron_tree = iondata.getNeuronTreeByID(i[:-7],i[-7:])\n",
    "    neuron_tree.somaRadius = 100\n",
    "    neuronvis.addNeuronTree(neuron_tree,axonColor=color_pool[j],somaColor=[0.5,0.5,0.5],dendriteColor=[0.5,0.5,0.5],mirrorToRight=True,isLine=False)\n",
    "neuronvis.addRegion('MRN')\n",
    "set_view(neuronvis,'anterior')\n",
    "neuronvis.render.savepng(motor_system_path+f'segment_3_integration.png')\n",
    "neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### coprojection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "co_projection = {}\n",
    "for i in range(8):\n",
    "    co_projection[i] = []\n",
    "    for j in group[i][0]:\n",
    "        co_projection[i].append(neuron_to_brainstem_information[j]['axonal_projection_terminal_combined'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in group:\n",
    "    save_scene(group[i][0],color_pool[i],motor_system_path+f'group_{i}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### visualization axon"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "info = {}\n",
    "for g in group:\n",
    "    info[g] = get_terminal_lines_in_mask(group[g][0],mask,color=color_pool[g])\n",
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear()\n",
    "for i in info:\n",
    "    draw_lines(neuronvis,info[i][0],set_color(info[i][1],color_pool[i]))\n",
    "neuronvis.render.setPointSize(1)\n",
    "for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "    set_view(neuronvis,i)\n",
    "    neuronvis.render.savepng(motor_system_path+'axon_v_'+i+'.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = []\n",
    "y = []\n",
    "for i in group:\n",
    "    for j in group[i][0]:\n",
    "        for terminal in neuron_terminals[j]['terminals']:\n",
    "            if if_inside_mask(mask,np.array(terminal)/10):\n",
    "                a = neuron_terminals[j]['mirror_mark']\n",
    "                x.append(terminal.copy())\n",
    "                if a:\n",
    "                    x[-1][2] = 11400-x[-1][2]\n",
    "                y.append(color_pool[i].tolist())\n",
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear()\n",
    "draw_points(neuronvis,x,y)\n",
    "neuronvis.render.setPointSize(3)\n",
    "for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "    set_view(neuronvis,i)\n",
    "    neuronvis.render.savepng(motor_system_path+'terminal_v_'+i+'.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear()\n",
    "for i in RF:\n",
    "    neuronvis.addRegion(i)\n",
    "neuronvis.render.setPointSize(3)\n",
    "for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "    set_view(neuronvis,i)\n",
    "    neuronvis.render.savepng(motor_system_path+'v_'+i+'.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### medulla respiration and vessel system"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mask,header = nrrd.read(motor_system_path+'RF.nrrd') \n",
    "info = {}\n",
    "for i in neuron_to_brainstem_information:\n",
    "    info[i] = {j+1:[] for j in range(9)}\n",
    "    for terminal in neuron_terminals[i]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            r = int(mask_2D[int(terminal[0]/10),int(terminal[1]/10)])\n",
    "            if r==0:\n",
    "                continue\n",
    "            a = neuron_terminals[i]['mirror_mark']\n",
    "            b = terminal[2]>5700\n",
    "            info[i][r].append(terminal.copy())\n",
    "            if info[i][r][-1][2]>5700:\n",
    "                info[i][r][-1][2] = 11400-info[i][r][-1][2]\n",
    "            if a!=b:\n",
    "                info[i][r][-1][2] = 11400-info[i][r][-1][2]\n",
    "background = [np.sum(mask[1064:1162],axis=0),np.sum(mask[1162:1233],axis=0),np.sum(mask[1233:],axis=0)]\n",
    "RF_list = ['PARN', 'IRN', 'GRN', 'MDRNd', 'MDRNv']\n",
    "mask = 0\n",
    "for i,j in enumerate(RF_list):\n",
    "    mask = np.where(get_region_mask(j),i+1,mask)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target_segment = [7,8,9]\n",
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear()\n",
    "for index,i in enumerate(target_segment):\n",
    "    X = []\n",
    "    for j in info:\n",
    "        if neuron_to_brainstem_information[j]['cortex_region'] not in ['MOp']:\n",
    "            continue\n",
    "        for k in info[j][i]:\n",
    "            X.append(k)\n",
    "    X = np.array(X)\n",
    "    draw_points(neuronvis,X,[[color_pool[index].tolist()] for j in X])\n",
    "#for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "#    set_view(neuronvis,i)\n",
    "#    neuronvis.render.savepng(overall_path+'dendrite_'+i+'.png')\n",
    "for i in ['MARN','PGRN','MDRN','DMX','VII','GRN']:\n",
    "    neuronvis.addRegion(i)\n",
    "neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### MOs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "statics = {}\n",
    "ss = [1162,1233]\n",
    "target_segment = [7,8,9]\n",
    "fig = plt.figure(figsize=(8,3))\n",
    "for index,i in enumerate(target_segment):\n",
    "    statics[i] = np.zeros((2,len(RF_list)))\n",
    "    X = []\n",
    "    for j in info:\n",
    "        if neuron_to_brainstem_information[j]['cortex_region'] not in ['MOs']:\n",
    "            continue\n",
    "        for k in info[j][i]:\n",
    "            r = int(mask[int(k[0]/10),int(k[1]/10),int(k[2]/10)])\n",
    "            if r>0:\n",
    "                statics[i][0 if k[2]<5700 else 1,r-1] += 1\n",
    "            X.append(k)\n",
    "    X = np.array(X)\n",
    "    ax = fig.add_subplot(2,len(target_segment),index+1)\n",
    "    ax.imshow(background[index]>0,cmap='gray')\n",
    "    ax.scatter(X[:,2]/10,X[:,1]/10,s=0.1)\n",
    "    ax.set_xlim(300,840)\n",
    "    ax.set_ylim(750,450)\n",
    "    ax.set_axis_off()\n",
    "    tmp = statics[i]*1\n",
    "    tmp[:,0] += tmp[:,3]\n",
    "    tmp[:,2] += tmp[:,4]\n",
    "    ax = fig.add_subplot(2,len(target_segment),len(target_segment)+index+1)\n",
    "    ax.barh(range(2,-1,-1),-tmp[0,:3],height=0.25,color=color_pool[3])\n",
    "    ax.barh(range(2,-1,-1),tmp[1,:3],height=0.25,color=color_pool[0])\n",
    "    if index==0:\n",
    "        ax.set_yticks(range(2,-1,-1),['lateral','intemediate','medial'])\n",
    "    else:\n",
    "        ax.set_yticks([],[])\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(motor_system_path+f'MOs_MYRN_S.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "statics = {}\n",
    "ss = [1162,1233]\n",
    "target_segment = [7,8,9]\n",
    "fig = plt.figure(figsize=(8,3))\n",
    "for index,i in enumerate(target_segment):\n",
    "    statics[i] = np.zeros((2,len(RF_list)))\n",
    "    X = []\n",
    "    for j in info:\n",
    "        if neuron_to_brainstem_information[j]['cortex_region'] not in ['MOp']:\n",
    "            continue\n",
    "        for k in info[j][i]:\n",
    "            r = int(mask[int(k[0]/10),int(k[1]/10),int(k[2]/10)])\n",
    "            if r>0:\n",
    "                statics[i][0 if k[2]<5700 else 1,r-1] += 1\n",
    "            X.append(k)\n",
    "    X = np.array(X)\n",
    "    ax = fig.add_subplot(2,len(target_segment),index+1)\n",
    "    ax.imshow(background[index]>0,cmap='gray')\n",
    "    ax.scatter(X[:,2]/10,X[:,1]/10,s=0.1)\n",
    "    ax.set_xlim(300,840)\n",
    "    ax.set_ylim(750,450)\n",
    "    ax.set_axis_off()\n",
    "    tmp = statics[i]*1\n",
    "    tmp[:,0] += tmp[:,3]\n",
    "    tmp[:,2] += tmp[:,4]\n",
    "    ax = fig.add_subplot(2,len(target_segment),len(target_segment)+index+1)\n",
    "    ax.barh(range(2,-1,-1),-tmp[0,:3],height=0.25,color=color_pool[3])\n",
    "    ax.barh(range(2,-1,-1),tmp[1,:3],height=0.25,color=color_pool[0])\n",
    "    if index==0:\n",
    "        ax.set_yticks(range(2,-1,-1),['lateral','intemediate','medial'])\n",
    "    else:\n",
    "        ax.set_yticks([],[])\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(motor_system_path+f'MOp_MYRN_S.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "statics = {}\n",
    "ss = [1162,1233]\n",
    "target_segment = [7,8,9]\n",
    "fig = plt.figure(figsize=(8,3))\n",
    "for index,i in enumerate(target_segment):\n",
    "    statics[i] = np.zeros((2,len(RF_list)))\n",
    "    X = []\n",
    "    for j in info:\n",
    "        if neuron_to_brainstem_information[j]['cortex_region'] not in ['AI']:\n",
    "            continue\n",
    "        for k in info[j][i]:\n",
    "            r = int(mask[int(k[0]/10),int(k[1]/10),int(k[2]/10)])\n",
    "            if r>0:\n",
    "                statics[i][0 if k[2]<5700 else 1,r-1] += 1\n",
    "            X.append(k)\n",
    "    X = np.array(X)\n",
    "    ax = fig.add_subplot(2,len(target_segment),index+1)\n",
    "    ax.imshow(background[index]>0,cmap='gray')\n",
    "    ax.scatter(X[:,2]/10,X[:,1]/10,s=0.1)\n",
    "    ax.set_xlim(300,840)\n",
    "    ax.set_ylim(750,450)\n",
    "    ax.set_axis_off()\n",
    "    tmp = statics[i]*1\n",
    "    tmp[:,0] += tmp[:,3]\n",
    "    tmp[:,2] += tmp[:,4]\n",
    "    ax = fig.add_subplot(2,len(target_segment),len(target_segment)+index+1)\n",
    "    ax.barh(range(2,-1,-1),-tmp[0,:3],height=0.25,color=color_pool[3])\n",
    "    ax.barh(range(2,-1,-1),tmp[1,:3],height=0.25,color=color_pool[0])\n",
    "    if index==0:\n",
    "        ax.set_yticks(range(2,-1,-1),['lateral','intemediate','medial'])\n",
    "    else:\n",
    "        ax.set_yticks([],[])\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(motor_system_path+f'AI_MYRN_S.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ratio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_labels = ['VIS','SS','AUD','MO','Cingulate','Insula','PFC'][::-1]\n",
    "tmp2 = {i:[0,0] for i in x_labels}\n",
    "for n,k in zip(neuron_to_brainstem_information,get_neuron_axon_info(cortex_neuron_name,terminal_or_length='terminal',if_combined=True,if_log=False,filter=RF)[1]):\n",
    "        r = cortical_module_transform[neuron_to_brainstem_information[n]['cortex_region']]\n",
    "        if r in tmp2:\n",
    "                if np.sum(k)>5:\n",
    "                        tmp2[r][0] += 1\n",
    "                tmp2[r][1] += 1\n",
    "for i in tmp2:\n",
    "    print(tmp2[i][0]/tmp2[i][1],tmp2[i][0],tmp2[i][1])\n",
    "x_labels = list(tmp2.keys())\n",
    "tmp = []\n",
    "for i in tmp2:\n",
    "    tmp.append(tmp2[i][0]/tmp2[i][1]*100)\n",
    "h = 0.5\n",
    "fig = plt.figure(figsize=(3,6))\n",
    "plt.barh(range(len(x_labels)),tmp,color=color_pool[0],height=h)\n",
    "plt.yticks(range(len(x_labels)),x_labels,fontsize=fontsize_ticks)\n",
    "plt.xlabel('Ratio (%)',fontsize=fontsize_label)\n",
    "plt.ylabel('Source',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.title('Neuron ratio',fontsize=fontsize_title)\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(motor_system_path+'Neuron_ratio.pdf',format='pdf')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Sensory vs Non-sensory"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = ['MRN']\n",
    "mask = 0.0\n",
    "for i in target:\n",
    "    mask += get_region_mask(i)\n",
    "X = {}\n",
    "for id,neuron in enumerate(neuron_to_brainstem_information):\n",
    "    module = cortical_module_transform[neuron_to_brainstem_information[neuron]['cortex_region']]\n",
    "    if module not in X:\n",
    "        X[module] = {'terminal':[]}\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            X[module]['terminal'].append(terminal.copy())\n",
    "            if neuron_terminals[neuron]['mirror_mark']:\n",
    "                X[module]['terminal'][-1][2] = 11400-X[module]['terminal'][-1][2]\n",
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "for i in target:\n",
    "    neuronvis.addRegion(i)\n",
    "for i in cortical_module:\n",
    "    if i in ['VIS','SS','AUD']:\n",
    "        draw_points(neuronvis,X[i]['terminal'],[color_pool[1].tolist() for j in X[i]['terminal']])\n",
    "    else:\n",
    "        draw_points(neuronvis,X[i]['terminal'],[color_pool[0].tolist() for j in X[i]['terminal']])\n",
    "neuronvis.render.setPointSize(1)\n",
    "for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "    set_view(neuronvis,i)\n",
    "    neuronvis.render.savepng(motor_system_path+'MRN_terminal_'+i+'.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = ['SCs','SCm','MRN','PAG']\n",
    "mask = 0.0\n",
    "for i in target:\n",
    "    mask += get_region_mask(i)\n",
    "X = {}\n",
    "for id,neuron in enumerate(neuron_to_brainstem_information):\n",
    "    module = cortical_module_transform[neuron_to_brainstem_information[neuron]['cortex_region']]\n",
    "    if module not in X:\n",
    "        X[module] = {'terminal':[]}\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            X[module]['terminal'].append(terminal.copy())\n",
    "            if neuron_terminals[neuron]['mirror_mark']:\n",
    "                X[module]['terminal'][-1][2] = 11400-X[module]['terminal'][-1][2]\n",
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "for i in target:\n",
    "    neuronvis.addRegion(i)\n",
    "for i in cortical_module:\n",
    "    if i in ['VIS','SS','AUD']:\n",
    "        draw_points(neuronvis,X[i]['terminal'],[color_pool[1].tolist() for j in X[i]['terminal']])\n",
    "    else:\n",
    "        draw_points(neuronvis,X[i]['terminal'],[color_pool[0].tolist() for j in X[i]['terminal']])\n",
    "neuronvis.render.setPointSize(1)\n",
    "for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "    set_view(neuronvis,i)\n",
    "    neuronvis.render.savepng(motor_system_path+'SCs_SCm_MRN_PAG_terminal_'+i+'.png')\n",
    "neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Integration hub"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "integration_system_path = data_path+'integration_system/'\n",
    "create_dir(integration_system_path)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## region visualization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Sensory_layer = ['PRT','SCs','SCm','IC','NLL','SOC','PB','PSV','CN','VNC','SPVO','SPVI','SPVC','AP','NTS','DCN']\n",
    "Central_gray_layer = ['PAG','PCG','LDT','NI']\n",
    "MN_midline = ['INC','ND','MA3','EW','III','Su3','IV','DTN','PDTg','SG','VI','PRP','DMX','XII']\n",
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "neuronvis.addRegion('MB')\n",
    "neuronvis.addRegion('P')\n",
    "neuronvis.addRegion('MY')\n",
    "for i in Sensory_layer:\n",
    "    neuronvis.addRegion(i,color=color_pool[3])\n",
    "for i in Central_gray_layer:\n",
    "    neuronvis.addRegion(i,color=color_pool[5])\n",
    "for i in MN_midline:\n",
    "    neuronvis.addRegion(i,color=color_pool[0])\n",
    "for i in ['dorsal','left']:\n",
    "    set_view(neuronvis,i)\n",
    "    if i=='dorsal':\n",
    "        neuronvis.render.setClip(False)\n",
    "    else:\n",
    "        neuronvis.render.setClip(True)\n",
    "    neuronvis.render.savepng(integration_system_path+'region_S_CG_MN_'+i+'.png')\n",
    "neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "neuronvis.addRegion('MB')\n",
    "neuronvis.addRegion('P')\n",
    "neuronvis.addRegion('MY')\n",
    "for i in Sensory_layer:\n",
    "    neuronvis.addRegion(i,color=color_pool[3])\n",
    "for i in Central_gray_layer:\n",
    "    neuronvis.addRegion(i,color=color_pool[5])\n",
    "neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "neuronvis.addRegion('MB')\n",
    "neuronvis.addRegion('P')\n",
    "neuronvis.addRegion('MY')\n",
    "for i in Sensory_layer:\n",
    "    neuronvis.addRegion(i,color=color_pool[3])\n",
    "for i in ['left']:\n",
    "    set_view(neuronvis,i)\n",
    "    if i=='dorsal':\n",
    "        neuronvis.render.setClip(False)\n",
    "    else:\n",
    "        neuronvis.render.setClip(True)\n",
    "    neuronvis.render.savepng(integration_system_path+'region_S_'+i+'.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "neuronvis.addRegion('MB')\n",
    "neuronvis.addRegion('P')\n",
    "neuronvis.addRegion('MY')\n",
    "for i in Central_gray_layer:\n",
    "    neuronvis.addRegion(i,color=color_pool[5])\n",
    "for i in ['left']:\n",
    "    set_view(neuronvis,i)\n",
    "    if i=='dorsal':\n",
    "        neuronvis.render.setClip(False)\n",
    "    else:\n",
    "        neuronvis.render.setClip(True)\n",
    "    neuronvis.render.savepng(integration_system_path+'region_CG_'+i+'.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## compare"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = Sensory_layer+Central_gray_layer\n",
    "compare_info = [get_axonal_statics_by_regions(i,target,'terminalregion') for i in cortex_neuron_name]\n",
    "X = {}\n",
    "for i,j in zip(cortex_neuron_name,compare_info):\n",
    "    region = neuron_to_brainstem_information[i]['cortex_region']\n",
    "    if region not in X:\n",
    "        X[region] = []\n",
    "    X[region].append(j)\n",
    "tmp = {}\n",
    "for i in cortical_module:\n",
    "    tmp[i] = []\n",
    "    for j in cortical_module[i]:\n",
    "        tmp[i] += X[j]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = []\n",
    "for i in tmp:\n",
    "    a += tmp[i]\n",
    "a = np.mean(a,axis=0)\n",
    "print(np.sum(a[:len(Sensory_layer)]),np.sum(a[len(Sensory_layer):]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_labels = ['VIS','SS','AUD','MO','Cingulate','Insula','TEa','PFC']\n",
    "colors = [1,1,1,0,0,0,0]\n",
    "width = 0.15\n",
    "step = 0.2\n",
    "step2 = 2\n",
    "fig = plt.figure(figsize=(6,4))\n",
    "ax = fig.add_subplot(1,1,1)\n",
    "y = []\n",
    "for i,j in enumerate(x_labels):\n",
    "    data = np.mean(tmp[j],axis=0)\n",
    "    data = np.array([np.sum(data[:len(Sensory_layer)]),np.sum(data[len(Sensory_layer):])]).astype(float)\n",
    "    y.append(data)\n",
    "y = np.array(y)\n",
    "ax.bar(range(len(y)),y[:,0],color=color_pool[3],label='Sensory nuclei',width=0.25)\n",
    "ax.bar(range(len(y)),y[:,1],bottom=y[:,0],color=color_pool[5],label='Central gray',width=0.25)\n",
    "ax.set_xticks(range(len(x_labels)),x_labels)\n",
    "ax.set_xlabel('Targets',fontsize=fontsize_label)\n",
    "ax.set_ylabel('Average terminal number',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "ax.set_title('Sensory nuclei vs central gray',fontsize=fontsize_title)\n",
    "plt.legend()\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(integration_system_path+'S_vs_CG.pdf',format='pdf')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## examples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def color_neuron_tree(neuron_tree,color_list):\n",
    "    for i in neuron_tree.points:\n",
    "        if i.type in color_list:\n",
    "            i.color = color_list[i.type]\n",
    "        else:\n",
    "            i.color = [0.5,0.5,0.5]\n",
    "    neuron_tree.root.color = [1,0.25,0.25]\n",
    "    #neuron_tree.somaColor = [1,0.25,0.25]\n",
    "neuron_list = ['233667159.swc','230109015.swc']\n",
    "color_list={1:[1,0.25,0.25],3:[0.859,0.267,0.216],13:color_pool[0].tolist(),18:color_pool[3].tolist()}\n",
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "for i in neuron_list:\n",
    "    neuron_tree = SwcLoader.NeuronTree()\n",
    "    neuron_tree.readFile(integration_system_path+i)\n",
    "    neuron_tree.dendriteHide = False\n",
    "    neuron_tree.somaRadius = 30\n",
    "    color_neuron_tree(neuron_tree,color_list=color_list)\n",
    "    neuronvis.addNeuronTree(neuron_tree,mirrorToRight=True,isLine=False)\n",
    "neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def color_neuron_tree(neuron_tree,color_list):\n",
    "    for i in neuron_tree.points:\n",
    "        if i.type in color_list:\n",
    "            i.color = color_list[i.type]\n",
    "        else:\n",
    "            i.color = [0.5,0.5,0.5]\n",
    "    neuron_tree.root.color = [1,0.25,0.25]\n",
    "    #neuron_tree.somaColor = [1,0.25,0.25]\n",
    "neuron_list = ['212073012.swc','221365102.swc']\n",
    "color_list={1:[1,0.25,0.25],3:[0.859,0.267,0.216],13:color_pool[0].tolist(),18:color_pool[3].tolist()}\n",
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "for i in neuron_list:\n",
    "    neuron_tree = SwcLoader.NeuronTree()\n",
    "    neuron_tree.readFile(integration_system_path+i)\n",
    "    neuron_tree.dendriteHide = False\n",
    "    neuron_tree.somaRadius = 30\n",
    "    color_neuron_tree(neuron_tree,color_list=color_list)\n",
    "    neuronvis.addNeuronTree(neuron_tree,mirrorToRight=True,isLine=False)\n",
    "neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## PRT vs SC vs IC"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_labels = ['VIS','SS','MO','Cingulate','AUD','TEa','Insula','PFC']\n",
    "colors = [1,1,1,0,0,0,0]\n",
    "width = 0.15\n",
    "step = 0.2\n",
    "step2 = 2\n",
    "fig = plt.figure(figsize=(5,4))\n",
    "ax = fig.add_subplot(1,1,1)\n",
    "y = []\n",
    "for i,j in enumerate(x_labels):\n",
    "    data = np.mean(tmp[j],axis=0)\n",
    "    data = np.array([data[target.index('PRT')],data[target.index('SCs')]+data[target.index('SCm')],data[target.index('IC')],data[target.index('PAG')]]).astype(float)\n",
    "    y.append(data)\n",
    "y = np.array(y)\n",
    "ax.bar(range(len(y)),y[:,3]/np.max(y[:,3]),color=color_pool[5],label='PAG',width=0.25)\n",
    "ax.bar(range(len(y)),y[:,2]/np.max(y[:,2]),bottom=1.5,color=color_pool[4],label='IC',width=0.25)\n",
    "ax.bar(range(len(y)),y[:,1]/np.max(y[:,1]),bottom=3,color=color_pool[1],label='SC',width=0.25)\n",
    "ax.bar(range(len(y)),y[:,0]/np.max(y[:,0]),bottom=4.5,color=color_pool[3],label='PRT',width=0.25)\n",
    "ax.set_xticks(range(len(x_labels)),x_labels)\n",
    "ax.set_xlabel('Targets',fontsize=fontsize_label)\n",
    "ax.set_ylabel('Average terminal number',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "ax.set_title('PRT vs SC vs IC vs APG',fontsize=fontsize_title)\n",
    "plt.legend()\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(integration_system_path+'PRT_vs_SC_vs_IC.pdf',format='pdf')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(4):\n",
    "    print(np.max(y[:,i]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## SC layer source origins"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = ['SCzo','SCsg','SCop','SCig','SCiw','SCdg','SCdw','ICe','ICd','ICc','PAG']\n",
    "info = {}\n",
    "for i in cortex_neuron_name:\n",
    "    d = neuron_to_brainstem_information[i]['soma_depth']\n",
    "    p = neuron_to_brainstem_information[i]['soma_flatten_position']\n",
    "    if len(neuron_to_brainstem_information[i]['soma_flatten_position'])>0:\n",
    "        info[i] = [d,p,get_axonal_statics_by_regions(i,target,'terminalregion')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for hg,tn in enumerate(target):\n",
    "    r = 200\n",
    "    s = 125\n",
    "    transition_color = np.array([[255,255,255],[161,238,204],[255,242,193],[246,49,141]])/255.0\n",
    "    cmap1 = mcolors.LinearSegmentedColormap.from_list('cmap1', transition_color)\n",
    "    kernel = generate_gaussian_kernel(r,s)\n",
    "    tmp = _flatmap[...,0]*0.0\n",
    "    soma_list = [[],[]]\n",
    "    for i in info:\n",
    "        d,p = info[i][:2]\n",
    "        if p==[]:\n",
    "            continue\n",
    "        e = np.sum(info[i][2][hg])\n",
    "        fill_kernel(tmp,np.array(p[::-1])*2,e/neuron_to_brainstem_information[i]['relative_density'],r,kernel)\n",
    "        soma_list[0].append(np.array(p[::-1])*2)\n",
    "        soma_list[1].append(e)\n",
    "    soma_list[1] = np.array(soma_list[1])**0.5\n",
    "    soma_list[1] /= np.max(soma_list[1])\n",
    "    #result = plot_transition_color(transition_color,tmp)\n",
    "    result = np.where(_flatmap[...,0]==-1,0,tmp)\n",
    "    for k,l in zip(soma_list[0],soma_list[1]):\n",
    "        cv.circle(result,[int(np.round(k[1])), int(np.round(k[0]))],int(np.round(l*10))+1,np.array([0.5,0.5,0.5]),-1)\n",
    "    fig = plt.figure(figsize=(7,5))\n",
    "    result /= np.max(result)\n",
    "    plt.imshow(result,cmap=cmap1,vmin=0,vmax=1)\n",
    "    plt.colorbar(extend='both')\n",
    "    plt.imshow(flatmap_mask,zorder=1)\n",
    "    plt.imshow(flatmap2,zorder=1)\n",
    "    plt.axis('off')\n",
    "    plt.xlim(flatten_map_r[0],flatten_map_r[1])\n",
    "    plt.ylim(flatten_map_r[2],flatten_map_r[3])\n",
    "    plt.title(f'{target[hg]}',fontsize=fontsize_title)\n",
    "    plt.rcParams['pdf.fonttype'] = 42\n",
    "    plt.rcParams['ps.fonttype'] = 42\n",
    "    plt.savefig(integration_system_path+f'{target[hg]}.pdf',format='pdf',bbox_inches='tight')\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cx = {'SCs':['SCzo','SCsg','SCop'],'SCi':['SCig','SCiw'],'SCd':['SCdg','SCdw'],'PAG':['PAG'],'ICs':['ICe','ICd'],'ICc':['ICc']}\n",
    "transition_color = np.array([[255,255,255],[161,238,204],[255,242,193],[246,49,141]])/255.0\n",
    "cmap1 = mcolors.LinearSegmentedColormap.from_list('cmap1', transition_color)\n",
    "r = 200\n",
    "s = 75\n",
    "kernel = generate_gaussian_kernel(r,s)\n",
    "for hg in cx:\n",
    "    tmp = _flatmap[...,0]*0.0\n",
    "    soma_list = [[],[]]\n",
    "    for i in info:\n",
    "        d,p = info[i][:2]\n",
    "        if p==[]:\n",
    "            continue\n",
    "        e = 0\n",
    "        for j in cx[hg]:\n",
    "            e += np.sum(info[i][2][target.index(j)])\n",
    "        fill_kernel(tmp,np.array(p[::-1])*2,e/neuron_to_brainstem_information[i]['relative_density'],r,kernel)\n",
    "        soma_list[0].append(np.array(p[::-1])*2)\n",
    "        soma_list[1].append(e)\n",
    "    soma_list[1] = np.array(soma_list[1])**0.5\n",
    "    soma_list[1] /= np.max(soma_list[1])\n",
    "    #result = plot_transition_color(transition_color,tmp)\n",
    "    result = np.where(_flatmap[...,0]==-1,0,tmp)\n",
    "    #for k,l in zip(soma_list[0],soma_list[1]):\n",
    "    #    cv.circle(result,[int(np.round(k[1])), int(np.round(k[0]))],int(np.round(l*10))+1,np.array([0.5,0.5,0.5]),-1)\n",
    "    fig = plt.figure(figsize=(7,5))\n",
    "    result /= np.max(result)\n",
    "    plt.imshow(result,cmap=cmap1,vmin=0,vmax=1)\n",
    "    plt.colorbar(extend='both')\n",
    "    plt.imshow(flatmap_mask,zorder=1)\n",
    "    plt.imshow(flatmap2,zorder=1)\n",
    "    plt.axis('off')\n",
    "    plt.xlim(flatten_map_r[0],flatten_map_r[1])\n",
    "    plt.ylim(flatten_map_r[2],flatten_map_r[3])\n",
    "    plt.title(f'{hg}',fontsize=fontsize_title)\n",
    "    plt.rcParams['pdf.fonttype'] = 42\n",
    "    plt.rcParams['ps.fonttype'] = 42\n",
    "    plt.savefig(integration_system_path+f'{hg}.pdf',format='pdf',bbox_inches='tight')\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## SC layer terminal distribution"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### SCs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "masks = ['SCs']\n",
    "color = {'VIS':color_pool[3].tolist(),'PTLp':color_pool[3].tolist()}\n",
    "mask = 0\n",
    "for i in masks:\n",
    "    mask += get_region_mask(i)\n",
    "result = {i:[[],[]] for i in color}\n",
    "for i in neuron_to_brainstem_information:\n",
    "    region = neuron_to_brainstem_information[i]['cortex_region']\n",
    "    if region not in color:\n",
    "        continue\n",
    "    for terminal in neuron_terminals[i]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            result[region][0].append(terminal.copy())\n",
    "            if neuron_terminals[i]['mirror_mark']:\n",
    "                result[region][0][-1][2] = 11400-result[region][0][-1][2]\n",
    "            result[region][1].append(color[region]) \n",
    "plt.matshow(get_contour(np.sum(mask,axis=1)>0),cmap='RdGy',vmin=-1,vmax=1)\n",
    "for i in result:\n",
    "    a = np.array(result[i][0])\n",
    "    plt.scatter(a[:,2]/10,a[:,0]/10,color=result[i][1],s=0.1)\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.xlim(570-230,570+230)\n",
    "plt.ylim(1070,800)\n",
    "plt.axis('off')\n",
    "plt.savefig(integration_system_path+'SCs.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### VISpv vs VISpd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = [[],[]]\n",
    "Y = [[],[]]\n",
    "plt.matshow(flatmap)\n",
    "for i in neuron_to_brainstem_information:\n",
    "    region = neuron_to_brainstem_information[i]['cortex_region']\n",
    "    if region not in ['VIS','PTLp']:\n",
    "        continue\n",
    "    p = neuron_to_brainstem_information[i]['soma_flatten_position']\n",
    "    if len(p)==0:\n",
    "        continue\n",
    "    if p[1]*2<2100:\n",
    "        Y[0].append(p)\n",
    "        X[0].append(i)\n",
    "    else:\n",
    "        Y[1].append(p)\n",
    "        X[1].append(i)\n",
    "plt.axhline(2100)\n",
    "Y[0] = np.array(Y[0])\n",
    "Y[1] = np.array(Y[1])\n",
    "plt.scatter(Y[0][:,0]*2,Y[0][:,1]*2,color=color_pool[0],s=1)\n",
    "plt.scatter(Y[1][:,0]*2,Y[1][:,1]*2,color=color_pool[3],s=1)\n",
    "plt.show()\n",
    "save_scene(X[0],color_pool[3],integration_system_path+'VISa')\n",
    "save_scene(X[1],color_pool[0],integration_system_path+'VISp')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A = [[],[]]\n",
    "for i in range(2):\n",
    "    for j in X[i]:\n",
    "        A[i].append(neuron_to_brainstem_information[j]['axonal_projection_terminal_combined'][brainstem_region.index('SCs')])\n",
    "plt.figure(figsize=(4,3))\n",
    "plt.hist(A[0],bins=100,cumulative=True,density=True,color=color_pool[0].tolist()+[0.4])\n",
    "plt.hist(np.array(A[1]),bins=100,cumulative=True,density=True,color=color_pool[3].tolist()+[0.4])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A = [[],[]]\n",
    "for i in range(2):\n",
    "    for j in X[i]:\n",
    "        A[i].append(neuron_to_brainstem_information[j]['axonal_projection_terminal_combined'][brainstem_region.index('STRd')])\n",
    "plt.figure(figsize=(4,3))\n",
    "plt.hist(A[0],bins=100,cumulative=True,density=True,color=color_pool[0].tolist()+[0.4])\n",
    "plt.hist(np.array(A[1]),bins=100,cumulative=True,density=True,color=color_pool[3].tolist()+[0.4])\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(integration_system_path+f'VIS_a_p_STRd.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A = [[],[]]\n",
    "for i in range(2):\n",
    "    for j in X[i]:\n",
    "        A[i].append(neuron_to_brainstem_information[j]['axonal_projection_terminal_combined'][brainstem_region.index('PG')])\n",
    "plt.figure(figsize=(4,3))\n",
    "plt.hist(A[0],bins=100,cumulative=True,density=True,color=color_pool[0].tolist()+[0.4])\n",
    "plt.hist(np.array(A[1]),bins=100,cumulative=True,density=True,color=color_pool[3].tolist()+[0.4])\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(integration_system_path+f'VIS_a_p_PG.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = ['SCs']\n",
    "mask = 0\n",
    "for i in target:\n",
    "    mask += get_region_mask(i)\n",
    "A = []\n",
    "B = []\n",
    "for i,c in zip(range(2),[0,3]):\n",
    "    for neuron in X[i]:\n",
    "        a = neuron_terminals[neuron]['mirror_mark']\n",
    "        for terminal in neuron_terminals[neuron]['terminals']:\n",
    "            if if_inside_mask(mask,np.array(terminal)/10):\n",
    "                A.append(terminal.copy())\n",
    "                B.append(color_pool[c].tolist())\n",
    "                if a:\n",
    "                    A[-1][2] = 11400-A[-1][2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "neuronvis.addRegion('SCs')\n",
    "draw_points(neuronvis,A,B)\n",
    "for i in ['dorsal']:\n",
    "    set_view(neuronvis,i)\n",
    "    neuronvis.render.savepng(overall_path+'dendrite_'+i+'.png')\n",
    "neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### SCi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "masks = ['SCig','SCiw']\n",
    "color = {'SSp':color_pool[1].tolist(),'AUD':color_pool[4],'VIS':color_pool[3],'PTLp':color_pool[3]}\n",
    "mask = 0\n",
    "for i in masks:\n",
    "    mask += get_region_mask(i)\n",
    "result = {i:[[],[]] for i in color}\n",
    "for i in neuron_to_brainstem_information:\n",
    "    region = neuron_to_brainstem_information[i]['cortex_region']\n",
    "    if region not in color:\n",
    "        continue\n",
    "    for terminal in neuron_terminals[i]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            result[region][0].append(terminal.copy())\n",
    "            if neuron_terminals[i]['mirror_mark']:\n",
    "                result[region][0][-1][2] = 11400-result[region][0][-1][2]\n",
    "            result[region][1].append(color[region]) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.matshow(get_contour(np.sum(mask,axis=1)>0),cmap='RdGy',vmin=-1,vmax=1)\n",
    "for i in result:\n",
    "    if i not in ['SSp']:\n",
    "        continue\n",
    "    a = np.array(result[i][0])\n",
    "    plt.scatter(a[:,2]/10,a[:,0]/10,color=result[i][1],s=0.1)\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.xlim(570-230,570+230)\n",
    "plt.ylim(1070,800)\n",
    "plt.axis('off')\n",
    "plt.savefig(integration_system_path+'SCi_SSp.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.matshow(get_contour(np.sum(mask,axis=1)>0),cmap='RdGy',vmin=-1,vmax=1)\n",
    "for i in result:\n",
    "    if i not in ['AUD']:\n",
    "        continue\n",
    "    a = np.array(result[i][0])\n",
    "    plt.scatter(a[:,2]/10,a[:,0]/10,color=result[i][1],s=0.1)\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.xlim(570-230,570+230)\n",
    "plt.ylim(1070,800)\n",
    "plt.axis('off')\n",
    "plt.savefig(integration_system_path+'SCi_AUD.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.matshow(get_contour(np.sum(mask,axis=1)>0),cmap='RdGy',vmin=-1,vmax=1)\n",
    "for i in result:\n",
    "    if i in ['SSp','AUD']:\n",
    "        continue\n",
    "    a = np.array(result[i][0])\n",
    "    plt.scatter(a[:,2]/10,a[:,0]/10,color=result[i][1],s=0.1)\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.xlim(570-230,570+230)\n",
    "plt.ylim(1070,800)\n",
    "plt.axis('off')\n",
    "plt.savefig(integration_system_path+'SCi_VIS.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [[],[]]\n",
    "for i in attention_neuron:\n",
    "    for terminal in neuron_terminals[i]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            a[0].append(terminal.copy())\n",
    "            if neuron_terminals[i]['mirror_mark']:\n",
    "                a[0][-1][2] = 11400-a[0][-1][2]\n",
    "            a[1].append(color_pool[0]) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#plt.matshow(get_contour(np.sum(mask,axis=1)>0),cmap='RdGy',vmin=-1,vmax=1)\n",
    "b = np.array(a[0])\n",
    "plt.scatter(b[:,2]/10,b[:,0]/10,color=a[1],s=0.1)\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.xlim(570-230,570+230)\n",
    "plt.ylim(1070,800)\n",
    "plt.axis('off')\n",
    "plt.savefig(integration_system_path+'SCi_FEF.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### SCd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "masks = ['SCdg','SCdw']\n",
    "color = {'TEa':color_pool[5].tolist(),'AUD':color_pool[5],'VISpor':color_pool[5],'VISli':color_pool[5],'RSP':color_pool[3].tolist(),'ACA':color_pool[0].tolist(),'MOs':color_pool[0].tolist()}\n",
    "mask = 0\n",
    "for i in masks:\n",
    "    mask += get_region_mask(i)\n",
    "result = {i:[[],[]] for i in color}\n",
    "for i in neuron_to_brainstem_information:\n",
    "    region = neuron_to_brainstem_information[i]['cortex_region']\n",
    "    if region=='VIS':\n",
    "        region += neuron_to_brainstem_information[i]['cortex_subregion']\n",
    "    if region not in color:\n",
    "        continue\n",
    "    for terminal in neuron_terminals[i]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            result[region][0].append(terminal.copy())\n",
    "            if neuron_terminals[i]['mirror_mark']:\n",
    "                result[region][0][-1][2] = 11400-result[region][0][-1][2]\n",
    "            result[region][1].append(color[region]) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.matshow(get_contour(np.sum(mask,axis=1)>0),cmap='RdGy',vmin=-1,vmax=1)\n",
    "for i in result:\n",
    "    if i in ['ACA','RSP','MOs']:\n",
    "        continue\n",
    "    a = np.array(result[i][0])\n",
    "    plt.scatter(a[:,2]/10,a[:,0]/10,color=result[i][1],s=0.1)\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.xlim(570-230,570+230)\n",
    "plt.ylim(1070,800)\n",
    "plt.axis('off')\n",
    "plt.savefig(integration_system_path+'SCd_TEa.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.matshow(get_contour(np.sum(mask,axis=1)>0),cmap='RdGy',vmin=-1,vmax=1)\n",
    "for i in result:\n",
    "    if i not in ['ACA','MOs']:\n",
    "        continue\n",
    "    a = np.array(result[i][0])\n",
    "    plt.scatter(a[:,2]/10,a[:,0]/10,color=result[i][1],s=0.1)\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.xlim(570-230,570+230)\n",
    "plt.ylim(1070,800)\n",
    "plt.axis('off')\n",
    "plt.savefig(integration_system_path+'SCd_ACA.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.matshow(get_contour(np.sum(mask,axis=1)>0),cmap='RdGy',vmin=-1,vmax=1)\n",
    "for i in result:\n",
    "    if i not in ['RSP']:\n",
    "        continue\n",
    "    a = np.array(result[i][0])\n",
    "    plt.scatter(a[:,2]/10,a[:,0]/10,color=result[i][1],s=0.1)\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.xlim(570-230,570+230)\n",
    "plt.ylim(1070,800)\n",
    "plt.axis('off')\n",
    "plt.savefig(integration_system_path+'SCd_RSP.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### PAG"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "masks = ['PAG']\n",
    "color = {'TEa':color_pool[5].tolist(),'ECT':color_pool[5].tolist(),'PERI':color_pool[5].tolist(),'AUD':color_pool[5].tolist(),'ACA':color_pool[0].tolist(),'MOs':color_pool[0].tolist()}\n",
    "mask = 0\n",
    "for i in masks:\n",
    "    mask += get_region_mask(i)\n",
    "result = {i:[[],[]] for i in color}\n",
    "for i in neuron_to_brainstem_information:\n",
    "    region = neuron_to_brainstem_information[i]['cortex_region']\n",
    "    if region not in color:\n",
    "        continue\n",
    "    for terminal in neuron_terminals[i]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            result[region][0].append(terminal.copy())\n",
    "            if neuron_terminals[i]['mirror_mark']:\n",
    "                result[region][0][-1][2] = 11400-result[region][0][-1][2]\n",
    "            result[region][1].append(color[region]) \n",
    "plt.matshow(get_contour(np.sum(mask,axis=0)>0),cmap='RdGy',vmin=-1,vmax=1)\n",
    "for i in ['MOs','ACA','TEa','ECT','AUD','PERI']:\n",
    "    a = np.array(result[i][0])\n",
    "    plt.scatter(a[:,2]/10,a[:,1]/10,color=result[i][1],s=0.2 if i in ['TEa','ECT','AUD','PERI'] else 0.1)\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.xlim(570-230,570+230)\n",
    "plt.ylim(500,150)\n",
    "plt.axis('off')\n",
    "plt.savefig(integration_system_path+'PAG.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Comparison"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mask = 0\n",
    "for i in ['SCs','SCm','IC']:\n",
    "    mask += get_region_mask(i)\n",
    "result = {i:[[],[]] for i in tmp}\n",
    "for i in neuron_to_brainstem_information:\n",
    "    try:\n",
    "        modality = cortical_module_transform[neuron_to_brainstem_information[i]['cortex_region']]\n",
    "    except:\n",
    "        continue\n",
    "    for terminal in neuron_terminals[i]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            result[modality][0].append(terminal.copy())\n",
    "            if neuron_terminals[i]['mirror_mark']:\n",
    "                result[modality][0][-1][2] = 11400-result[modality][0][-1][2]\n",
    "            result[modality][1].append(color[modality]) \n",
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear()\n",
    "for i in result:\n",
    "    if i=='VIS':\n",
    "        continue\n",
    "    draw_points(neuronvis,result[i][0],result[i][1])\n",
    "set_view(neuronvis,'dorsal')\n",
    "neuronvis.render.savepng(integration_system_path+'VIS_SS_AUD.png')\n",
    "neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## SC layer statics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = []\n",
    "target = ['SCzo','SCsg','SCop','SCig','SCiw','SCdg','SCdw','ICe','ICd','ICc','PAG']\n",
    "tmp = {}\n",
    "for i in neuron_to_brainstem_information:\n",
    "    region = neuron_to_brainstem_information[i]['cortex_region']+neuron_to_brainstem_information[i]['cortex_subregion']\n",
    "    if neuron_to_brainstem_information[i]['cortex_region']=='SSp':\n",
    "        region = 'SSp'\n",
    "    if region not in tmp:\n",
    "        tmp[region] = []\n",
    "    tmp[region].append(get_axonal_statics_by_regions(i,target,'terminalregion'))\n",
    "for i in tmp:\n",
    "    tmp[i] = np.mean(tmp[i],axis=0)\n",
    "    norm = np.linalg.norm(tmp[i])\n",
    "    if norm==0:\n",
    "        norm = 1\n",
    "    tmp[i] = tmp[i]/norm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "order = {i:[] for i,j in enumerate(target)}\n",
    "for i in tmp:\n",
    "    order[np.argmax(tmp[i])].append(f'{i}+{np.max(tmp[i])}')\n",
    "sequence = []\n",
    "for i,j in enumerate(target):\n",
    "    a = [k.split('+') for k in order[i]]\n",
    "    s = sorted(a,key=lambda x:x[1],reverse=False)\n",
    "    sequence += [k[0] for k in s]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sequence = []\n",
    "layer_vector = (np.array([i for i in range(len(target))])+1)*1\n",
    "sequence = [[i,np.sum(tmp[i]/np.sum(tmp[i])*layer_vector)] for i in tmp]\n",
    "sequence = sorted(sequence,key=lambda x:x[1],reverse=False)\n",
    "sequence = [i[0] for i in sequence]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sequence = ['VISp','VISpl','VISl','VISal','VISpor','VISpm','VISli','PTLprl','PTLpa','RSPd','VISam','RSPagl','RSPv','ACAv','ACAd','VISC','GU','MOp','SSp','MOs','FRP','SSs','AUDpo','AUDd','AUDv','AUDp','TEa','AId','PL','ECT','ORBl','AIp','ORBvl','ILA','ORBm','PERI','AIv']\n",
    "result = np.array([tmp[i] for i in sequence]).T\n",
    "plt.figure(figsize=(5,4))\n",
    "plt.matshow(result,cmap='BuGn')\n",
    "_ = plt.yticks(range(len(target)),target,fontsize=fontsize_ticks)\n",
    "_ = plt.xticks(range(len(sequence)),sequence,fontsize=fontsize_ticks,rotation=-90)\n",
    "plt.xlabel('Modality',fontsize=fontsize_label)\n",
    "plt.ylabel('Terminal number (normalized)',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "#plt.title('Projection preference',fontsize=fontsize_title)\n",
    "plt.savefig(integration_system_path+'SC_layer_statics.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.colorbar()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sequence = ['VISp','VISpl','VISpor','VISl','VISli','VISal','PTLprl','PTLpa','TEa']\n",
    "result = np.array([tmp[i] for i in sequence]).T\n",
    "plt.figure(figsize=(5,4))\n",
    "plt.matshow(result,cmap='BuGn')\n",
    "_ = plt.yticks(range(len(target)),target,fontsize=fontsize_ticks)\n",
    "_ = plt.xticks(range(len(sequence)),sequence,fontsize=fontsize_ticks,rotation=-90)\n",
    "plt.xlabel('Modality',fontsize=fontsize_label)\n",
    "plt.ylabel('Terminal number (normalized)',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "#plt.title('Projection preference',fontsize=fontsize_title)\n",
    "#plt.savefig(integration_system_path+'SC_layer_statics.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.colorbar()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in source:\n",
    "    neuron_info = get_axonal_statics_by_regions(i,brainstem_region,'terminalregion')\n",
    "    a = np.sum(neuron_info[1],axis=0)\n",
    "    result.append(a/np.linalg.norm(a))\n",
    "tmp = [[i,j] for i,j in zip(source,result)]\n",
    "tmp = sorted(tmp,key=lambda x:x[1][0]*3+x[1][1]*1+x[1][2]*0,reverse=True)\n",
    "source = []\n",
    "result = []\n",
    "for i in tmp:\n",
    "    source.append(i[0])\n",
    "    result.append(i[1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## SC output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "info = {}\n",
    "target2 = ['CTX','CNU','HY','TH','MB','P','MY','unknow','RT','ZI','PRT']\n",
    "for i in target+['PAG']:\n",
    "    info[i] = []\n",
    "    neuron_list = iondata.getNeuronListBySomaRegion(i)\n",
    "    for j in neuron_list:\n",
    "        if len(j['sampleid']+j['name'])!=13:\n",
    "            continue\n",
    "        tmp = get_axonal_statics_by_regions(j['sampleid']+j['name'],target2,'terminalregion')\n",
    "        tmp = [tmp[0],tmp[1],tmp[2]-tmp[-2],tmp[-3]+tmp[-2],tmp[3]-tmp[-3],tmp[-1],tmp[4],tmp[5],tmp[6],tmp[7]]\n",
    "        info[i].append(tmp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target3 = ['CTX','CNU','HY','PTH','TH','PRT','MB','P','MY','Spine']\n",
    "X = np.zeros((len(target+['PAG']),len(target3)))\n",
    "for index,i in enumerate(info):\n",
    "    if len(info[i])==0 or index==0:\n",
    "        continue\n",
    "    tmp = np.mean(info[i],axis=0)\n",
    "    print(i,len(info[i]),np.round(tmp/np.sum(tmp)*100,0))\n",
    "    X[index] = tmp#/np.sum(tmp)*100\n",
    "plt.matshow(X,cmap='coolwarm')\n",
    "plt.xticks(range(len(target3)),target3)\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.colorbar()\n",
    "plt.savefig(integration_system_path+'SC_layer_output.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## dectection modules"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mask = get_region_mask('SCs')\n",
    "plt.matshow(np.sum(mask,axis=1))\n",
    "plt.axvline(450)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mask = 0\n",
    "for i,j in enumerate(['SCs','SCm','IC']):\n",
    "    mask = np.where(get_region_mask(j),i+1,mask)\n",
    "color_transform = {'environmental':0,'self':3}\n",
    "info = {}\n",
    "for i in neuron_to_brainstem_information:\n",
    "    d = neuron_to_brainstem_information[i]['soma_depth']\n",
    "    p = neuron_to_brainstem_information[i]['soma_flatten_position']\n",
    "    if 0 in p or len(p)==0:\n",
    "        continue\n",
    "    info[i] = [d,p,0,0]\n",
    "    for terminal in neuron_terminals[i]['terminals']:\n",
    "        v = int(get_mask_value(mask,np.array(terminal)/10))\n",
    "        if v==0:\n",
    "            continue\n",
    "        elif v==2:\n",
    "            if abs(terminal[2]-5700)<1200:\n",
    "                info[i][2] += 1\n",
    "            else:\n",
    "                info[i][3] += 1\n",
    "        else:\n",
    "            info[i][2] += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result,soma_list = get_pdf(_flatmap[...,0],neuron_to_brainstem_information,info)\n",
    "a = _flatmap*0.0\n",
    "b = []\n",
    "for i,j in zip(color_transform,result):\n",
    "    c = np.expand_dims(j/(np.max(j) if np.max(j)!=0 else 1),axis=-1)\n",
    "    b.append([c,c*color_pool[color_transform[i]]])\n",
    "c = np.sum([i[0] for i in b],axis=0)\n",
    "c = np.where(c==0.0,1.0,c)\n",
    "merged_result = 0.0\n",
    "for i in b:\n",
    "    merged_result += i[0]/c*i[1]\n",
    "d = np.max([i[0] for i in b],axis=0)\n",
    "tmp = merged_result+(1-d)*np.array([1.0,1.0,1.0])\n",
    "for i in soma_list:\n",
    "    for k,l in zip(i[0],i[1]):\n",
    "        cv.circle(tmp,[int(np.round(k[1])), int(np.round(k[0]))],int(np.round(l*10))+1,np.array([0.5,0.5,0.5]),-1)\n",
    "fig,ax = plt.subplots(figsize=(5,5))\n",
    "ax.imshow(tmp,zorder=0)\n",
    "ax.imshow(flatmap_mask,zorder=1)\n",
    "ax.imshow(flatmap,zorder=2)\n",
    "ax.set_xlim(flatten_map_r[0],flatten_map_r[1])\n",
    "ax.set_ylim(flatten_map_r[2],flatten_map_r[3])\n",
    "ax.set_title('Dectection modules',color='k',fontsize=fontsize_title)\n",
    "ax.set_axis_off()\n",
    "ax.legend(handles=[mpatches.Patch(color=color_pool[color_transform[i]], label=i) for i in color_transform],loc='lower right')\n",
    "plt.savefig(integration_system_path+'detection_modules.pdf',format='pdf',bbox_inches='tight',dpi=100)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "for i,j in zip([1,3],[0,3]):\n",
    "    for k in Scene.scene2List('cluster_'+str(i)+'.nv'):\n",
    "        neuron_tree = iondata.getNeuronTreeByID(k['sampleid'],k['name'])\n",
    "        neuron_tree.somaRadius = 30\n",
    "        neuronvis.addNeuronTree(neuron_tree,axonColor=color_pool[j],somaColor=[0.5,0.5,0.5],mirrorToRight=True,somaHide=False,dendriteHide=True)\n",
    "for j in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "    set_view(neuronvis,j)\n",
    "    neuronvis.render.savepng(integration_system_path+'SC_projection_'+j+'.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=False)\n",
    "for i,j in zip(['211181003.swc','211181036.swc'],[0,3]):\n",
    "    neuron_tree = iondata.getNeuronTreeByID(i[:-7],i[-7:])\n",
    "    neuron_tree.somaRadius = 100\n",
    "    neuronvis.addNeuronTree(neuron_tree,axonColor=color_pool[j],somaColor=[0.5,0.5,0.5],dendriteColor=[0.5,0.5,0.5],mirrorToRight=True,isLine=False)\n",
    "for k in ['anterior','posterior','dorsal']:\n",
    "    set_view(neuronvis,k)\n",
    "    neuronvis.render.savepng(integration_system_path+'SC_examples_projection_'+k+'.png')\n",
    "neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## SC dendrite"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = {}\n",
    "for i in range(3):\n",
    "    for neuron in Scene.scene2List(integration_system_path+f'SCm_{i+1}.nv'):\n",
    "        neuron_tree = iondata.getNeuronTreeByID(neuron['sampleid'],neuron['name'])\n",
    "        X[neuron['sampleid']+neuron['name']] = {'soma':[0,0,0],'dendrite':[]}\n",
    "        for edge in neuron_tree.getDendrite():\n",
    "            tmp = []\n",
    "            for point in edge.data:\n",
    "                tmp.append(point.xyz)\n",
    "            X[neuron['sampleid']+neuron['name']]['dendrite'].append(np.array(tmp))\n",
    "        X[neuron['sampleid']+neuron['name']]['soma'] = np.array(neuron_tree.root.xyz)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### direction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.decomposition import PCA\n",
    "Y = {}\n",
    "for i in X:\n",
    "    Y[i] = {}\n",
    "    a = []\n",
    "    for j in X[i]['dendrite']:\n",
    "        a += j.tolist()\n",
    "    if len(a)==0:\n",
    "        continue\n",
    "    pca = PCA(n_components=3)\n",
    "    a = pca.fit(np.array(a))\n",
    "    Y[i]['explained_variance_ratio_'] = a.explained_variance_ratio_\n",
    "    Y[i]['components_'] = a.components_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def angle_between_vectors(u, v):\n",
    "    dot_product = np.dot(u, v)\n",
    "    magnitude_u = np.linalg.norm(u)\n",
    "    magnitude_v = np.linalg.norm(v)\n",
    "    cos_theta = np.min([dot_product / (magnitude_u * magnitude_v),1])\n",
    "    angle_radians = np.arccos(cos_theta)\n",
    "    angle_degrees = np.degrees(angle_radians)\n",
    "    return angle_degrees\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target_vector = np.array([1.0,1.0,1.0])\n",
    "target_vector /= np.linalg.norm(target_vector)\n",
    "tmp = []\n",
    "for i in Y:\n",
    "    if Y[i]=={}:\n",
    "        continue\n",
    "    tmp.append(angle_between_vectors(target_vector,Y[i]['explained_variance_ratio_']))\n",
    "parameter=[0,55//2,2,5]\n",
    "plt.figure(figsize=(15,5))\n",
    "distribution_discrete,distribution_continuous = estimate_density_function(tmp,parameter)\n",
    "plt.plot(distribution_continuous,color=color_pool[0])\n",
    "plt.bar(range(parameter[1]),distribution_discrete,color=color_pool[0])\n",
    "plt.xlim(parameter[0],parameter[1])\n",
    "plt.xticks(np.array([angle_between_vectors(target_vector,np.array([1.0,1.0,1.0])),angle_between_vectors(target_vector,np.array([1.0,1.0,0.0])),angle_between_vectors(target_vector,np.array([1.0,0.0,0.0]))])/2,['Linear','Plane','Spherical'])\n",
    "plt.xlabel('Spherial <---> Plan <---> Line index',fontsize=15)\n",
    "plt.ylabel('Probability',fontsize=15,rotation=-90).set_verticalalignment('top')\n",
    "plt.title('Shape distribution',fontsize=17)\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(integration_system_path+'SC_dendrite_shape.pdf',format='pdf')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target_vector = np.array([0.0,1.0,0.0])\n",
    "target_vector /= np.linalg.norm(target_vector)\n",
    "tmp = []\n",
    "for i in Y:\n",
    "    if Y[i]=={}:\n",
    "        continue\n",
    "    tmp.append(angle_between_vectors(target_vector,Y[i]['components_'][0]))\n",
    "    if tmp[-1]>90:\n",
    "        tmp[-1] = 180-tmp[-1]\n",
    "parameter=[0,45,2,7]\n",
    "plt.figure(figsize=(15,5))\n",
    "distribution_discrete,distribution_continuous = estimate_density_function(tmp,parameter)\n",
    "plt.plot(distribution_continuous,color=color_pool[0])\n",
    "plt.bar(range(parameter[1]),distribution_discrete,color=color_pool[0])\n",
    "plt.xlim(parameter[0],parameter[1])\n",
    "plt.xticks(np.array([0,45,90])/2,['Vertical','Diagnal','Horizontal'])\n",
    "plt.xlabel('Spherial <---> Plan <---> Line index',fontsize=15)\n",
    "plt.ylabel('Probability',fontsize=15,rotation=-90).set_verticalalignment('top')\n",
    "plt.title('Direction distribution',fontsize=17)\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(integration_system_path+'SC_dendrite_direction.pdf',format='pdf')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### visualization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "SCm_mask_2D_lateral = get_contour(np.sum(get_region_mask('SCm'),axis=2)>0)\n",
    "SCm_mask_2D_anterior = get_contour(np.sum(get_region_mask('SCm'),axis=0)>0)\n",
    "SCm_mask_2D_dorsal = get_contour(np.sum(get_region_mask('SCm'),axis=1)>0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.decomposition import PCA\n",
    "for i in X:\n",
    "    fig = plt.figure(figsize=(20,5))\n",
    "    plt.matshow(SCm_mask_2D,cmap='RdGy',vmin=-1,vmax=1)\n",
    "    if np.random.rand()<0.:\n",
    "        continue\n",
    "    color = np.random.rand(3)\n",
    "    a = []\n",
    "    for j in X[i]['dendrite']:\n",
    "        plt.plot(j[:,2]/10,j[:,1]/10,color=color,linewidth=0.5)\n",
    "        a += (np.array([j[:,2]/10,j[:,1]/10]).transpose(1,0)).tolist()\n",
    "    pca = PCA(n_components=2)\n",
    "    a = pca.fit(np.array(a)/10)\n",
    "    b = a.singular_values_\n",
    "    a = a.components_\n",
    "    plt.scatter([X[i]['soma'][2]/10],[X[i]['soma'][1]/10],s=10,color=[0.25,0.25,0.25])\n",
    "    p = np.array([X[i]['soma'][2]/10,X[i]['soma'][1]/10])\n",
    "    plt.plot([p[0]-a[0][0]/2*b[0],p[0]+a[0][0]/2*b[0]],[p[1]-a[0][1]/2*b[0],p[1]+a[0][1]/2*b[0]],color=color_pool[0])\n",
    "    plt.plot([p[0]-a[1][0]/2*b[1],p[0]+a[1][0]/2*b[1]],[p[1]-a[1][1]/2*b[0],p[1]+a[1][1]/2*b[1]],color=color_pool[3])\n",
    "    plt.xlim(300,840)\n",
    "    plt.ylim(400,100)\n",
    "    plt.axis('off')\n",
    "    plt.rcParams['pdf.fonttype'] = 42\n",
    "    plt.rcParams['ps.fonttype'] = 42\n",
    "    plt.savefig(integration_system_path+f'SCm_dendrite_{i}.pdf',format='pdf',bbox_inches='tight')\n",
    "    plt.show()\n",
    "    break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.decomposition import PCA\n",
    "for i in X:\n",
    "    try:\n",
    "        fig = plt.figure(figsize=(10,10))\n",
    "        plt.matshow(SCm_mask_2D.T,cmap='RdGy',vmin=-1,vmax=1)\n",
    "        if np.random.rand()<0.:\n",
    "            continue\n",
    "        color = np.random.rand(3)\n",
    "        a = []\n",
    "        for j in X[i]['dendrite']:\n",
    "            plt.plot(j[:,0]/10,j[:,1]/10,color=color,linewidth=0.5)\n",
    "            a += (np.array([j[:,0]/10,j[:,1]/10]).transpose(1,0)).tolist()\n",
    "        pca = PCA(n_components=2)\n",
    "        a = pca.fit(np.array(a)/10)\n",
    "        b = a.singular_values_\n",
    "        a = a.components_\n",
    "        plt.scatter([X[i]['soma'][0]/10],[X[i]['soma'][1]/10],s=10,color=[0.25,0.25,0.25])\n",
    "        plt.title(i)\n",
    "        plt.xlim(790,1050)\n",
    "        plt.ylim(380,130)\n",
    "        #plt.axis('off')\n",
    "        plt.rcParams['pdf.fonttype'] = 42\n",
    "        plt.rcParams['ps.fonttype'] = 42\n",
    "        plt.savefig(integration_system_path+f'SCm_dendrite_{i}.pdf',format='pdf',bbox_inches='tight')\n",
    "        plt.show()\n",
    "    except:\n",
    "        pass"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### apical/basal dendrite"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_dendrite(neuron):\n",
    "    neuron_tree = SwcLoader.NeuronTree()\n",
    "    neuron_tree.readFile(integration_system_path+f'SC_neuron/{neuron}')\n",
    "    result = {'apical_dendrite':[],'basal_dendrite':[]}\n",
    "    dendrites=[]\n",
    "    for edge in neuron_tree.edges:\n",
    "            if edge.data[1].type not in [3,4]:\n",
    "                continue\n",
    "            dendrites.append(edge)\n",
    "    for edge in dendrites:\n",
    "        tmp = []\n",
    "        for point in edge.data:\n",
    "            tmp.append(point.xyz.copy())\n",
    "        type = 'basal_dendrite' if edge.data[-1].type==3 else 'apical_dendrite'\n",
    "        result[type].append(np.array(tmp))\n",
    "    return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuron_list = ['211179002.swc','211179003.swc','211180009.swc','211181001.swc','211181002.swc','211181003.swc','211181018.swc','211181040.swc','211181041.swc','211181042.swc','220985004.swc','211182012.swc',]\n",
    "for neuron in neuron_list:\n",
    "    if 'apical_dendrite' not in X[neuron]:\n",
    "        tmp = get_dendrite(neuron)\n",
    "        X[neuron]['apical_dendrite'] = tmp['apical_dendrite']\n",
    "        X[neuron]['basal_dendrite'] = tmp['basal_dendrite']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.decomposition import PCA\n",
    "for i in ['220985004.swc']:\n",
    "    try:\n",
    "        fig,axes = plt.subplots(1,3,figsize=(450,150))\n",
    "        axes[0].matshow(SCm_mask_2D_anterior,cmap='RdGy',vmin=-1,vmax=1)\n",
    "        for j in X[i]['apical_dendrite']:\n",
    "            axes[0].plot(j[:,2]/10,j[:,1]/10,color=color_pool[0],linewidth=0.5)\n",
    "        for j in X[i]['basal_dendrite']:\n",
    "            axes[0].plot(j[:,2]/10,j[:,1]/10,color=color_pool[3],linewidth=0.5)\n",
    "        axes[0].scatter([X[i]['soma'][2]/10],[X[i]['soma'][1]/10],s=10,color=[0.25,0.25,0.25])\n",
    "        axes[0].set_title(i)\n",
    "        axes[0].set_xlim(300,800)\n",
    "        axes[0].set_ylim(380,130)\n",
    "        axes[0].set_axis_off()\n",
    "\n",
    "\n",
    "        axes[1].matshow(SCm_mask_2D_lateral.T,cmap='RdGy',vmin=-1,vmax=1)\n",
    "        for j in X[i]['apical_dendrite']:\n",
    "            axes[1].plot(j[:,0]/10,j[:,1]/10,color=color_pool[0],linewidth=0.5)\n",
    "        for j in X[i]['basal_dendrite']:\n",
    "            axes[1].plot(j[:,0]/10,j[:,1]/10,color=color_pool[3],linewidth=0.5)\n",
    "        axes[1].scatter([X[i]['soma'][0]/10],[X[i]['soma'][1]/10],s=10,color=[0.25,0.25,0.25])\n",
    "        axes[1].set_title(i)\n",
    "        axes[1].set_xlim(600,1100)\n",
    "        axes[1].set_ylim(380,130)\n",
    "        axes[1].set_axis_off()\n",
    "\n",
    "\n",
    "        axes[2].matshow(SCm_mask_2D_dorsal,cmap='RdGy',vmin=-1,vmax=1)\n",
    "        for j in X[i]['apical_dendrite']:\n",
    "            axes[2].plot(j[:,2]/10,j[:,0]/10,color=color_pool[0],linewidth=0.5)\n",
    "        for j in X[i]['basal_dendrite']:\n",
    "            axes[2].plot(j[:,2]/10,j[:,0]/10,color=color_pool[3],linewidth=0.5)\n",
    "        axes[2].scatter([X[i]['soma'][2]/10],[X[i]['soma'][0]/10],s=10,color=[0.25,0.25,0.25])\n",
    "        axes[2].set_title(i)\n",
    "        axes[2].set_xlim(300,800)\n",
    "        axes[2].set_ylim(1050,800)\n",
    "        axes[2].set_axis_off()\n",
    "        \n",
    "\n",
    "\n",
    "        plt.rcParams['pdf.fonttype'] = 42\n",
    "        plt.rcParams['ps.fonttype'] = 42\n",
    "        plt.savefig(integration_system_path+f'SCm_dendrite_{i}.pdf',format='pdf',bbox_inches='tight')\n",
    "        plt.show()\n",
    "    except:\n",
    "        pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.decomposition import PCA\n",
    "for i in neuron_list:\n",
    "    try:\n",
    "        fig,axes = plt.subplots(1,3,figsize=(15,5))\n",
    "        axes[0].matshow(SCm_mask_2D_anterior,cmap='RdGy',vmin=-1,vmax=1)\n",
    "        for j in X[i]['apical_dendrite']:\n",
    "            axes[0].plot(j[:,2]/10,j[:,1]/10,color=color_pool[0],linewidth=0.5)\n",
    "        for j in X[i]['basal_dendrite']:\n",
    "            axes[0].plot(j[:,2]/10,j[:,1]/10,color=color_pool[3],linewidth=0.5)\n",
    "        axes[0].scatter([X[i]['soma'][2]/10],[X[i]['soma'][1]/10],s=10,color=[0.25,0.25,0.25])\n",
    "        axes[0].set_title(i)\n",
    "        axes[0].set_xlim(300,800)\n",
    "        axes[0].set_ylim(380,130)\n",
    "        axes[0].set_axis_off()\n",
    "\n",
    "\n",
    "        axes[1].matshow(SCm_mask_2D_lateral.T,cmap='RdGy',vmin=-1,vmax=1)\n",
    "        for j in X[i]['apical_dendrite']:\n",
    "            axes[1].plot(j[:,0]/10,j[:,1]/10,color=color_pool[0],linewidth=0.5)\n",
    "        for j in X[i]['basal_dendrite']:\n",
    "            axes[1].plot(j[:,0]/10,j[:,1]/10,color=color_pool[3],linewidth=0.5)\n",
    "        axes[1].scatter([X[i]['soma'][0]/10],[X[i]['soma'][1]/10],s=10,color=[0.25,0.25,0.25])\n",
    "        axes[1].set_title(i)\n",
    "        axes[1].set_xlim(600,1100)\n",
    "        axes[1].set_ylim(380,130)\n",
    "        axes[1].set_axis_off()\n",
    "\n",
    "\n",
    "        axes[2].matshow(SCm_mask_2D_dorsal,cmap='RdGy',vmin=-1,vmax=1)\n",
    "        for j in X[i]['apical_dendrite']:\n",
    "            axes[2].plot(j[:,2]/10,j[:,0]/10,color=color_pool[0],linewidth=0.5)\n",
    "        for j in X[i]['basal_dendrite']:\n",
    "            axes[2].plot(j[:,2]/10,j[:,0]/10,color=color_pool[3],linewidth=0.5)\n",
    "        axes[2].scatter([X[i]['soma'][2]/10],[X[i]['soma'][0]/10],s=10,color=[0.25,0.25,0.25])\n",
    "        axes[2].set_title(i)\n",
    "        axes[2].set_xlim(300,800)\n",
    "        axes[2].set_ylim(1050,800)\n",
    "        axes[2].set_axis_off()\n",
    "        \n",
    "\n",
    "\n",
    "        plt.rcParams['pdf.fonttype'] = 42\n",
    "        plt.rcParams['ps.fonttype'] = 42\n",
    "        plt.savefig(integration_system_path+f'SCm_dendrite_{i}.pdf',format='pdf',bbox_inches='tight')\n",
    "        plt.show()\n",
    "    except:\n",
    "        pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "SC_mask = 0\n",
    "SC_layers = ['SCzo','SCsg','SCop','SCig','SCiw','SCdg','SCdw','PAG']\n",
    "for i,j in enumerate(SC_layers):\n",
    "    SC_mask = np.where(get_region_mask(j),i+1,SC_mask)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "info = {}\n",
    "for neuron in neuron_list:\n",
    "    info[neuron] = {'apical_dendrite':np.zeros((len(SC_layers,))),'basal_dendrite':np.zeros((len(SC_layers,)))}\n",
    "    for edge in X[neuron]['apical_dendrite']:\n",
    "        for terminal in edge:\n",
    "            v = int(get_mask_value(SC_mask,np.array(terminal)/10))\n",
    "            if v>0:\n",
    "                info[neuron]['apical_dendrite'][v-1] += 1\n",
    "    for edge in X[neuron]['basal_dendrite']:\n",
    "        for terminal in edge:\n",
    "            v = int(get_mask_value(SC_mask,np.array(terminal)/10))\n",
    "            if v>0:\n",
    "                info[neuron]['basal_dendrite'][v-1] += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in info:\n",
    "    plt.bar(range(len(SC_layers)),info[i]['apical_dendrite'],color=color_pool[0])\n",
    "    plt.bar(range(len(SC_layers)),-info[i]['basal_dendrite'],color=color_pool[3])\n",
    "    plt.xticks(range(len(SC_layers)),SC_layers)\n",
    "    v = int(get_mask_value(SC_mask,np.array(X[i]['soma'])/10))\n",
    "    plt.title(f'{i}_{SC_layers[v-1]}')\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.decomposition import PCA\n",
    "for i in X:\n",
    "    try:\n",
    "        a = []\n",
    "        for j in X[i]['dendrite']:\n",
    "            a += (np.array([j[:,2]/10,j[:,1]/10]).transpose(1,0)).tolist()\n",
    "        pca = PCA(n_components=2)\n",
    "        a = pca.fit(np.array(a)/10)\n",
    "        b = a.singular_values_\n",
    "        a = a.components_\n",
    "        X[i]['direction'] = a\n",
    "        X[i]['sigular_value'] = b\n",
    "    except:\n",
    "        continue"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = []\n",
    "for i in X:\n",
    "    try:\n",
    "        a.append(X[i]['sigular_value'])\n",
    "    except:\n",
    "        pass\n",
    "a = np.array(a)\n",
    "r = a[:,1]/a[:,0]\n",
    "plt.hist(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.decomposition import PCA\n",
    "fig = plt.figure(figsize=(20,5))\n",
    "plt.matshow(SCm_mask_2D,cmap='RdGy',vmin=-1,vmax=1)\n",
    "for i in X:\n",
    "    try:\n",
    "        if X[i]['sigular_value'][1]/X[i]['sigular_value'][0]<0.7:\n",
    "            continue\n",
    "        color = np.random.rand(3)\n",
    "        a = []\n",
    "        for j in X[i]['dendrite']:\n",
    "            #plt.plot(j[:,2]/10,j[:,1]/10,color=color,linewidth=0.5)\n",
    "            a += (np.array([j[:,2]/10,j[:,1]/10]).transpose(1,0)).tolist()\n",
    "        pca = PCA(n_components=2)\n",
    "        a = pca.fit(np.array(a)/10)\n",
    "        b = a.singular_values_\n",
    "        a = a.components_\n",
    "        #plt.scatter([X[i]['soma'][2]/10],[X[i]['soma'][1]/10],s=10,color=[0.25,0.25,0.25])\n",
    "        p = np.array([X[i]['soma'][2]/10,X[i]['soma'][1]/10])\n",
    "        #plt.plot([p[0]-a[0][0]/2*b[0],p[0]+a[0][0]/2*b[0]],[p[1]-a[0][1]/2*b[0],p[1]+a[0][1]/2*b[0]],color=color_pool[0],linewidth=0.25)\n",
    "        plt.plot([p[0]-a[1][0]/2*b[1],p[0]+a[1][0]/2*b[1]],[p[1]-a[1][1]/2*b[0],p[1]+a[1][1]/2*b[1]],color=color_pool[3],linewidth=0.25)\n",
    "    except:\n",
    "        pass\n",
    "plt.xlim(300,840)\n",
    "plt.ylim(400,100)\n",
    "plt.axis('off')\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(integration_system_path+f'SCm_dendrite.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in X:\n",
    "    soma = np.array(X[i]['soma'])\n",
    "    dendrite_group = {}\n",
    "    c = 0\n",
    "    for j in X[i]['dendrite']:\n",
    "        k = np.array(j)\n",
    "        if np.sum(k[0]-soma)**2==0:\n",
    "            dendrite_group[c] = [k]\n",
    "            c += 1\n",
    "        else:\n",
    "            for l in dendrite_group:\n",
    "                for m in dendrite_group[l]:\n",
    "                    if np.sum(k[0]-m[-1])**2==0:\n",
    "                        dendrite_group[l].append(k)\n",
    "    plt.matshow(SCm_mask_2D,cmap='RdGy',vmin=-1,vmax=1)\n",
    "    for j,k in enumerate(dendrite_group):\n",
    "        for l in dendrite_group[k]:\n",
    "            m = np.array(l)\n",
    "            plt.plot(m[:,2]/10,m[:,1]/10,color=color_pool[j],linewidth=0.5)\n",
    "    plt.xlim(300,840)\n",
    "    plt.ylim(400,100)\n",
    "    plt.axis('off')\n",
    "    plt.title(i)\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in ['211181030.swc']:\n",
    "    print(i)\n",
    "    for j in X[i]['dendrite']:\n",
    "        k = np.array(j)\n",
    "        plt.plot(k[:,2],k[:,1],color=color_pool[0])\n",
    "    break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#fig = plt.figure(figsize=(20,5))\n",
    "#plt.matshow(SCm_mask_2D,cmap='RdGy',vmin=-1,vmax=1)\n",
    "for i,j in enumerate(dendrite_group):\n",
    "    for k in dendrite_group[j]:\n",
    "        l = np.array(k)\n",
    "        plt.plot(l[:,2]/10,l[:,1]/10,color=color_pool[i])\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Pons"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "for i in [['PB','PSV','NLL','SOC'],['PCG','LDT','PDTg','DTN','NI','SG']]:\n",
    "    neuronvis.clear(root=True)\n",
    "    neuronvis.addRegion('P')\n",
    "    for j in i:\n",
    "        neuronvis.addRegion(j,color_pool[0])\n",
    "    set_view(neuronvis,'anterior')\n",
    "    neuronvis.render.savepng(integration_system_path+f'{\"_\".join(i)}_region.png')\n",
    "neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = ['PB','PSV','NLL','PCG','LDT','PDTg','DTN','NI','SG']\n",
    "info = {}\n",
    "for i in cortex_neuron_name:\n",
    "    d = neuron_to_brainstem_information[i]['soma_depth']\n",
    "    p = neuron_to_brainstem_information[i]['soma_flatten_position']\n",
    "    info[i] = [d,p,get_axonal_statics_by_regions(i,target,'terminalregion')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cx = {'PB-PSV-NLL':['PB','PSV','NLL'],'PCG':['PCG','LDT','PDTg','DTN','NI','SG']}\n",
    "transition_color = np.array([[255,255,255],[161,238,204],[255,242,193],[246,49,141]])/255.0\n",
    "cmap1 = mcolors.LinearSegmentedColormap.from_list('cmap1', transition_color)\n",
    "r = 200\n",
    "s = 125\n",
    "kernel = generate_gaussian_kernel(r,s)\n",
    "for hg in cx:\n",
    "    tmp = _flatmap[...,0]*0.0\n",
    "    soma_list = [[],[]]\n",
    "    for i in cortex_neuron_name:\n",
    "        d,p = info[i][:2]\n",
    "        if p==[]:\n",
    "            continue\n",
    "        e = 0\n",
    "        for j in cx[hg]:\n",
    "            e += np.sum(info[i][2][target.index(j)])\n",
    "        fill_kernel(tmp,np.array(p[::-1])*2,e/neuron_to_brainstem_information[i]['relative_density'],r,kernel)\n",
    "        soma_list[0].append(np.array(p[::-1])*2)\n",
    "        soma_list[1].append(e)\n",
    "    soma_list[1] = np.array(soma_list[1])**0.5\n",
    "    soma_list[1] /= np.max(soma_list[1])\n",
    "    #result = plot_transition_color(transition_color,tmp)\n",
    "    result = np.where(_flatmap[...,0]==-1,0,tmp)\n",
    "    for k,l in zip(soma_list[0],soma_list[1]):\n",
    "        cv.circle(result,[int(np.round(k[1])), int(np.round(k[0]))],int(np.round(l*10))+1,np.array([0.5,0.5,0.5]),-1)\n",
    "    fig = plt.figure(figsize=(7,5))\n",
    "    result /= np.max(result)\n",
    "    plt.imshow(result,cmap=cmap1,vmin=0,vmax=1)\n",
    "    plt.colorbar(extend='both')\n",
    "    plt.imshow(flatmap_mask,zorder=1)\n",
    "    plt.imshow(flatmap2,zorder=1)\n",
    "    plt.axis('off')\n",
    "    plt.xlim(flatten_map_r[0],flatten_map_r[1])\n",
    "    plt.ylim(flatten_map_r[2],flatten_map_r[3])\n",
    "    plt.title(f'{hg}',fontsize=fontsize_title)\n",
    "    plt.rcParams['pdf.fonttype'] = 42\n",
    "    plt.rcParams['ps.fonttype'] = 42\n",
    "    plt.savefig(integration_system_path+f'{hg}.pdf',format='pdf',bbox_inches='tight')\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "index_list = {'PSV':[brainstem_region.index(i) for i in ['PSV']],'PB':[brainstem_region.index(i) for i in ['PB']],'NLL':[brainstem_region.index(i) for i in ['NLL']]}\n",
    "color_transform = {'PB':5,'NLL':2,'PSV':1}\n",
    "info = {}\n",
    "for i in neuron_to_brainstem_information:\n",
    "    d = neuron_to_brainstem_information[i]['soma_depth']\n",
    "    p = neuron_to_brainstem_information[i]['soma_flatten_position']\n",
    "    if p==[]:\n",
    "        continue\n",
    "    info[i] = [d,p]\n",
    "    for j in index_list:\n",
    "        tmp = 0.0\n",
    "        for k in index_list[j]:\n",
    "            tmp += neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][k]\n",
    "        info[i].append(tmp)\n",
    "result,soma_list = get_pdf(_flatmap[...,0],neuron_to_brainstem_information,info)\n",
    "a = _flatmap*0.0\n",
    "b = []\n",
    "for i,j in zip(index_list,result):\n",
    "    c = np.expand_dims(j/np.max(j),axis=-1)\n",
    "    b.append([c,c*color_pool[color_transform[i]]])\n",
    "c = np.sum([i[0] for i in b],axis=0)\n",
    "c = np.where(c==0.0,1.0,c)\n",
    "merged_result = 0.0\n",
    "for i in b:\n",
    "    merged_result += i[0]/c*i[1]\n",
    "d = np.max([i[0] for i in b],axis=0)\n",
    "tmp = merged_result+(1-d)*np.array([1.0,1.0,1.0])\n",
    "for i in soma_list:\n",
    "    for k,l in zip(i[0],i[1]):\n",
    "        cv.circle(tmp,[int(np.round(k[1])), int(np.round(k[0]))],int(np.round(l*10))+1,np.array([0.5,0.5,0.5]),-1)\n",
    "fig,ax = plt.subplots(figsize=(5,5))\n",
    "ax.imshow(tmp,zorder=0)\n",
    "ax.imshow(flatmap_mask,zorder=1)\n",
    "ax.imshow(flatmap2,zorder=1)\n",
    "ax.set_xlim(flatten_map_r[0],flatten_map_r[1])\n",
    "ax.set_ylim(flatten_map_r[2],flatten_map_r[3])\n",
    "ax.set_title('NLL-PB-PSV',color='k',fontsize=fontsize_title)\n",
    "ax.set_axis_off()\n",
    "ax.legend(handles=[mpatches.Patch(color=color_pool[color_transform[i]], label=i) for i in color_transform],loc='lower right')\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(integration_system_path+'NLL-PB_PSV.pdf',format='pdf',bbox_inches='tight',dpi=100)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Medulla"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "for i in [['SPVO','SPVI','SPVC','CN','VNC'],['AP','NTS','DMX']]:\n",
    "    neuronvis.clear(root=True)\n",
    "    neuronvis.addRegion('MY')\n",
    "    for j in i:\n",
    "        neuronvis.addRegion(j,color_pool[0])\n",
    "    set_view(neuronvis,'anterior')\n",
    "    neuronvis.render.savepng(integration_system_path+f'{\"_\".join(i)}_region.png')\n",
    "neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Modulator system"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "modulator_system_path = data_path+'modulator_system/'\n",
    "create_dir(modulator_system_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = ['SNc','VTA','DR']\n",
    "b = get_neuron_axon_info(cortex_neuron_name,terminal_or_length='terminal',if_combined = True,if_log = False,filter=a)[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig,ax = plt.subplots(figsize=(5,5))\n",
    "ax.bar(range(len(a)),np.average(b,axis=0))\n",
    "ax.set_xticks(range(len(a)),a)\n",
    "ax.set_title('Dopaminergic and Acetylcholinergic system',color='k',fontsize=fontsize_title)\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(modulator_system_path+'Terminal_strength_in_modulator.pdf',format='pdf',bbox_inches='tight',dpi=100)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Region visualization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Dopaminergic = ['SNc','VTA']\n",
    "#Noradrenalinergic = ['LC']\n",
    "#Cholinergic = ['PPN','LDT']\n",
    "Serotoninergic = ['DR']\n",
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.render.setClip(False)\n",
    "neuronvis.clear(root=True)\n",
    "neuronvis.addRegion('MB')\n",
    "for i in Dopaminergic:\n",
    "    neuronvis.addRegion(i,color=color_pool[3])\n",
    "for i in Serotoninergic:\n",
    "    neuronvis.addRegion(i,color=color_pool[5])\n",
    "neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Compare"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = ['SNc','VTA','DR']\n",
    "compare_info = get_neuron_axon_info(cortex_neuron_name,terminal_or_length='terminal',if_combined=True,if_log=False,filter=target)[1]\n",
    "X = {}\n",
    "for i,j in zip(cortex_neuron_name,compare_info):\n",
    "    region = neuron_to_brainstem_information[i]['cortex_region']\n",
    "    if region not in X:\n",
    "        X[region] = []\n",
    "    X[region].append(j)\n",
    "tmp = {}\n",
    "for i in cortical_module:\n",
    "    tmp[i] = []\n",
    "    for j in cortical_module[i]:\n",
    "        tmp[i] += X[j]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_labels = ['VIS','SS','AUD','MO','Cingulate','Insula','TEa','PFC']\n",
    "colors = [1,1,1,0,0,0,0]\n",
    "width = 0.15\n",
    "step = 0.2\n",
    "step2 = 2\n",
    "fig = plt.figure(figsize=(6,4))\n",
    "ax = fig.add_subplot(1,1,1)\n",
    "y = []\n",
    "for i,j in enumerate(x_labels):\n",
    "    data = np.mean(tmp[j],axis=0)\n",
    "    y.append(data)\n",
    "y = np.array(y)\n",
    "ax.bar(range(len(y)),y[:,0],color=color_pool[3],label='SNc',width=0.25)\n",
    "ax.bar(range(len(y)),y[:,1],bottom=y[:,0],color=color_pool[0],label='VTA',width=0.25)\n",
    "ax.bar(range(len(y)),y[:,2],bottom=y[:,0]+y[:,1],color=color_pool[5],label='DR',width=0.25)\n",
    "ax.set_xticks(range(len(x_labels)),x_labels)\n",
    "ax.set_xlabel('Targets',fontsize=fontsize_label)\n",
    "ax.set_ylabel('Average terminal number',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "ax.set_title('Dopaminergic vs serotonergic',fontsize=fontsize_title)\n",
    "plt.legend()\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(modulator_system_path+'D_vs_S.pdf',format='pdf')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i,j in zip(['221227167.swc','231567165.swc'],[3,1]):\n",
    "    neuronvis = nv.neuronVis(size=(1200,900))\n",
    "    neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "    set_view(neuronvis,'anterior')\n",
    "    neuron_tree = iondata.getNeuronTreeByID(i[:-7],i[-7:])\n",
    "    neuron_tree.somaRadius = 90\n",
    "    neuronvis.addNeuronTree(neuron_tree,axonColor=color_pool[j].tolist(),somaColor=[0.5,0.5,0.5],dendriteColor=[0.5,0.5,0.5],mirrorToRight=True,isLine=False)\n",
    "    for k in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "        set_view(neuronvis,k)\n",
    "        neuronvis.render.savepng(modulator_system_path+f'example_{i[:-4]}_{k}.png')\n",
    "    neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Source"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = ['SNc','VTA','DR']\n",
    "info = {}\n",
    "for i in cortex_neuron_name:\n",
    "    d = neuron_to_brainstem_information[i]['soma_depth']\n",
    "    p = neuron_to_brainstem_information[i]['soma_flatten_position']\n",
    "    info[i] = [d,p,np.zeros((len(target)),)]\n",
    "    for j,k in enumerate(target):\n",
    "        info[i][2][j] = neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][brainstem_region.index(k)]\n",
    "for hg,tn in enumerate(target):\n",
    "    r = 200\n",
    "    s = 75\n",
    "    transition_color = np.array([[255,255,255],[161,238,204],[255,242,193],[246,49,141]])/255.0\n",
    "    cmap1 = mcolors.LinearSegmentedColormap.from_list('cmap1', transition_color)\n",
    "    kernel = generate_gaussian_kernel(r,s)\n",
    "    tmp = _flatmap[...,0]*0.0\n",
    "    soma_list = [[],[]]\n",
    "    for i in info:\n",
    "        d,p = info[i][:2]\n",
    "        if p==[]:\n",
    "            continue\n",
    "        e = np.sum(info[i][2][hg])\n",
    "        fill_kernel(tmp,np.array(p[::-1])*2,e/neuron_to_brainstem_information[i]['relative_density'],r,kernel)\n",
    "        soma_list[0].append(np.array(p[::-1])*2)\n",
    "        soma_list[1].append(e)\n",
    "    soma_list[1] = np.array(soma_list[1])**0.5\n",
    "    soma_list[1] /= np.max(soma_list[1])\n",
    "    #result = plot_transition_color(transition_color,tmp)\n",
    "    result = np.where(_flatmap[...,0]==-1,0,tmp)\n",
    "    #for k,l in zip(soma_list[0],soma_list[1]):\n",
    "    #    cv.circle(result,[int(np.round(k[1])), int(np.round(k[0]))],int(np.round(l*10))+1,np.array([0.5,0.5,0.5]),-1)\n",
    "    fig = plt.figure(figsize=(7,5))\n",
    "    result /= np.max(result)\n",
    "    plt.imshow(result,cmap=cmap1,vmin=0,vmax=1)\n",
    "    plt.colorbar(extend='both')\n",
    "    plt.imshow(flatmap_mask,zorder=1)\n",
    "    plt.imshow(flatmap2,zorder=1)\n",
    "    plt.axis('off')\n",
    "    plt.xlim(flatten_map_r[0],flatten_map_r[1])\n",
    "    plt.ylim(flatten_map_r[2],flatten_map_r[3])\n",
    "    plt.title(f'{target[hg]}',fontsize=fontsize_title)\n",
    "    plt.rcParams['pdf.fonttype'] = 42\n",
    "    plt.rcParams['ps.fonttype'] = 42\n",
    "    plt.savefig(modulator_system_path+f'{target[hg]}.pdf',format='pdf',bbox_inches='tight')\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## learning system"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "info = {}\n",
    "for i in neuron_to_brainstem_information:\n",
    "    region = neuron_to_brainstem_information[i]['cortex_region']\n",
    "    if region not in info:\n",
    "        info[region] = [0,0,0,0,0,0,0]\n",
    "    info[region][0] += 1\n",
    "    info[region][1] += neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][brainstem_region.index('ACB')]\n",
    "    info[region][2] += neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][brainstem_region.index('STRd')]\n",
    "    info[region][3] += np.sum(neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][brainstem_region.index('AAA'):brainstem_region.index('AAA')+3])\n",
    "    info[region][4] += np.sum(neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][brainstem_region.index('LA'):brainstem_region.index('LA')+4])\n",
    "    info[region][5] += neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][brainstem_region.index('COA')]\n",
    "    info[region][6] += neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][brainstem_region.index('ENT')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = ['ACB','STRd','cnuAMY','spAMY','olfAMY','ENT']\n",
    "x = ['ACA','PTLp','SSp','SSs','MOp','MOs','AUD','RSP','VIS','GU','VISC','TEa','FRP','ORB','ILA','PL','AI','PERI','ECT']\n",
    "y = np.array([info[i] for i in x])\n",
    "norm = np.where(y[:,0]==0,1,y[:,0])\n",
    "fig = plt.figure(figsize=(9,4))\n",
    "for i,j in enumerate(target):\n",
    "    ax = fig.add_subplot(1,len(target),i+1)\n",
    "    ax.barh(range(len(x)),width=y[:,i+1]/norm)\n",
    "    if i==0:\n",
    "        ax.set_yticks(range(len(x)),x)\n",
    "    else:\n",
    "        ax.set_yticks([],[])\n",
    "    ax.set_title(j)\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(modulator_system_path+'Learning_system.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "info = {}\n",
    "for i in neuron_to_brainstem_information:\n",
    "    region = neuron_to_brainstem_information[i]['cortex_region']\n",
    "    if region not in info:\n",
    "        info[region] = [0,0,0,0,0]\n",
    "    info[region][0] += 1\n",
    "    info[region][1] += neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][brainstem_region.index('LSX')]\n",
    "    info[region][2] += neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][brainstem_region.index('ACB')]\n",
    "    info[region][3] += np.sum(neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][brainstem_region.index('AAA'):brainstem_region.index('AAA')+3])\n",
    "    info[region][4] += neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][brainstem_region.index('STRd')]\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = ['LSX','ACB','sAMY','STRd']\n",
    "x = ['ACA','PTLp','SSp','SSs','MOp','MOs','AUD','RSP','VIS','TEa','ECT','GU','VISC','PERI','AI','FRP','ILA','PL','ORB']\n",
    "y = np.array([info[i] for i in x])\n",
    "norm = np.where(y[:,0]==0,1,y[:,0])\n",
    "fig = plt.figure(figsize=(9,4))\n",
    "for i,j in enumerate(target):\n",
    "    ax = fig.add_subplot(1,len(target),i+1)\n",
    "    ax.barh(range(len(x)),width=y[:,i+1]/norm)\n",
    "    if i==0:\n",
    "        ax.set_yticks(range(len(x)),x)\n",
    "    else:\n",
    "        ax.set_yticks([],[])\n",
    "    ax.set_title(j)\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(modulator_system_path+'Learning_system.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## coming from different cells"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = flatmap2[...,:3]*1\n",
    "attention_neuron = []\n",
    "n = 0\n",
    "for i in neuron_to_brainstem_information:\n",
    "    d = neuron_to_brainstem_information[i]['soma_depth']\n",
    "    p = neuron_to_brainstem_information[i]['soma_flatten_position']\n",
    "    if len(p)==0:\n",
    "        continue\n",
    "    if np.linalg.norm(np.array([2100,650])-np.array(p)*2)<175:\n",
    "        n += 1\n",
    "        cv.circle(a,[int(np.round(p[0]))*2, int(np.round(p[1]))*2],10,np.array([1.0,0.25,0.25]),-1)\n",
    "        attention_neuron.append(i)\n",
    "plt.imshow(a)\n",
    "plt.show()\n",
    "print(len(attention_neuron))\n",
    "tmp = []\n",
    "for i in attention_neuron:\n",
    "    tmp.append([np.sum(neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][brainstem_region.index('IF'):brainstem_region.index('IF')+5]),neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][brainstem_region.index('VTA')]])\n",
    "tmp = np.array(tmp)\n",
    "group = {i:[[],[]] for i in range(2)}\n",
    "for i,j in zip(attention_neuron,tmp):\n",
    "    if np.sum(j)==0:\n",
    "        continue\n",
    "    group[np.argmax(j)][0].append(i)\n",
    "    group[np.argmax(j)][1].append(j/np.sum(j))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp2 = []\n",
    "for i in range(2):\n",
    "    tmp2 += group[i][1]\n",
    "tmp2 = np.array(tmp2)\n",
    "plt.matshow(tmp2.T,cmap='Purples',vmin=0,vmax=1)\n",
    "plt.colorbar()\n",
    "plt.xlabel(f'Neurons (n=42)',fontsize=fontsize_label)\n",
    "plt.ylabel(f'Targets',fontsize=fontsize_label)\n",
    "plt.title(f'ORBm in VTA and RAmb',fontsize=fontsize_title)\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(modulator_system_path+f'ORBm_VTA_RAmb_source.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "group[0][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "save_scene(group[0][0],color_pool[0],modulator_system_path+'ORBm_1')\n",
    "save_scene(group[1][0],color_pool[3],modulator_system_path+'ORBm_2')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## economic system"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "info = {}\n",
    "for i in neuron_to_brainstem_information:\n",
    "    region = neuron_to_brainstem_information[i]['cortex_region']\n",
    "    if region not in info:\n",
    "        info[region] = [0,0,0,0,0,0,0]\n",
    "    info[region][0] += 1\n",
    "    info[region][1] += np.sum(neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][brainstem_region.index('IF'):brainstem_region.index('IF')+5])\n",
    "    info[region][2] += neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][brainstem_region.index('CS')]+neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][brainstem_region.index('RPO')]\n",
    "    info[region][3] += np.sum(neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][brainstem_region.index('RM'):brainstem_region.index('RM')+3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = ['MB raphe','P raphe','MY raphe']\n",
    "x = ['FRP','PL','ORB','AI','ILA','ACA','RSP','PTLp','VIS','ECT','PERI','TEa','AUD','SSs','SSp','GU','VISC','MOp','MOs'][::-1]\n",
    "y = np.array([info[i] for i in x])\n",
    "norm = np.where(y[:,0]==0,1,y[:,0])\n",
    "fig = plt.figure(figsize=(9,4))\n",
    "for i,j in enumerate(target):\n",
    "    ax = fig.add_subplot(1,len(target),i+1)\n",
    "    ax.barh(range(len(x)),width=y[:,i+1]/norm)\n",
    "    if i==0:\n",
    "        ax.set_yticks(range(len(x)),x)\n",
    "    else:\n",
    "        ax.set_yticks([],[])\n",
    "    ax.set_title(j)\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(modulator_system_path+'Economic_system.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## coprojection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp = {'Raphe':['DR'],'SNc':['SNc'],'VTA':['VTA']}\n",
    "_c = {'Raphe':5,'SNc':3,'VTA':0}\n",
    "info = {}\n",
    "for system in tmp:\n",
    "    info[system] = []\n",
    "    for i in neuron_to_brainstem_information:\n",
    "        s = 0\n",
    "        for j in tmp[system]:\n",
    "            s += neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][brainstem_region.index(j)]\n",
    "        if s>5:\n",
    "            info[system].append(i)\n",
    "for i in info:\n",
    "    save_scene(info[i],color_pool[_c[i]],modulator_system_path+i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### visualization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "for i in tmp:\n",
    "    if i=='Noradrenergic':\n",
    "        continue\n",
    "    for k in Scene.scene2List(modulator_system_path+i+'.nv'):\n",
    "        neuron_tree = iondata.getNeuronTreeByID(k['sampleid'],k['name'])\n",
    "        neuron_tree.somaRadius = 30\n",
    "        neuronvis.addNeuronTree(neuron_tree,axonColor=color_pool[_c[i]],somaColor=[0.5,0.5,0.5],mirrorToRight=True,somaHide=False,dendriteHide=True)\n",
    "for j in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "    set_view(neuronvis,j)\n",
    "    neuronvis.render.savepng(modulator_system_path+'modulator_coprojection_'+j+'.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i,j in zip(['221424010.swc','233282036.swc','231567165.swc'],[5,3,1]):\n",
    "    neuronvis = nv.neuronVis(size=(1200,900))\n",
    "    neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "    set_view(neuronvis,'anterior')\n",
    "    neuron_tree = iondata.getNeuronTreeByID(i[:-7],i[-7:])\n",
    "    neuron_tree.somaRadius = 90\n",
    "    neuronvis.addNeuronTree(neuron_tree,axonColor=color_pool[j],somaColor=[0.5,0.5,0.5],dendriteColor=[0.5,0.5,0.5],mirrorToRight=True,isLine=False)\n",
    "    for k in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "        set_view(neuronvis,k)\n",
    "        neuronvis.render.savepng(modulator_system_path+f'example_{i[:-4]}_{k}.png')\n",
    "    neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i,j in zip(['233667190.swc'],[3]):\n",
    "    neuronvis = nv.neuronVis(size=(1200,900))\n",
    "    neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "    set_view(neuronvis,'anterior')\n",
    "    neuron_tree = iondata.getNeuronTreeByID(i[:-7],i[-7:])\n",
    "    neuron_tree.somaRadius = 90\n",
    "    neuronvis.addNeuronTree(neuron_tree,axonColor=color_pool[j],somaColor=[0.5,0.5,0.5],dendriteColor=[0.5,0.5,0.5],mirrorToRight=True,isLine=False)\n",
    "    for k in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "        set_view(neuronvis,k)\n",
    "        neuronvis.render.savepng(modulator_system_path+f'example_{i[:-4]}_{k}.png')\n",
    "    neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in ['221424010.swc','233282036.swc','231567165.swc']:\n",
    "    print(neuron_to_brainstem_information[i]['cortex_region'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### statics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp = {'Raphe':['DR'],'SNc':['SNc'],'VTA':['VTA']}\n",
    "info = {}\n",
    "for system in tmp:\n",
    "    info[system] = []\n",
    "    for ii in Scene.scene2List(modulator_system_path+system+'.nv'):\n",
    "        i = ii['sampleid']+ii['name']\n",
    "        if system!='Noradrenergic':\n",
    "            s = 0\n",
    "            for j in tmp[system]:\n",
    "                s += neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][brainstem_region.index(j)]\n",
    "            s /= np.sum(neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'])\n",
    "        else:\n",
    "            s = LC_strength[i]/np.sum(neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'])\n",
    "        info[system].append(np.array(neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'])*s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### BS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "BS_region_list = {'MB':brainstem_region_hierachy['MB'],'P':brainstem_region_hierachy['P'],'MY':brainstem_region_hierachy['MY']}\n",
    "targets = []\n",
    "for i in BS_region_list:\n",
    "    targets += BS_region_list[i]\n",
    "tmp = {'Raphe':['DR'],'SNc':['SNc'],'VTA':['VTA']}\n",
    "info = {}\n",
    "for system in tmp:\n",
    "    info[system] = []\n",
    "    for ii in Scene.scene2List(modulator_system_path+system+'.nv'):\n",
    "        i = ii['sampleid']+ii['name']\n",
    "        info[system].append(get_neuron_axon_info([i],terminal_or_length='terminal',if_combined = True,if_log = False,filter=targets)[1][0])\n",
    "fig = plt.figure(figsize=(15,3))\n",
    "for i in range(1,3):\n",
    "    plt.axhline(i+1,linestyle='--',color='#AAAAAA')\n",
    "_c = {'SNc':3,'VTA':0,'Raphe':5}\n",
    "for index,i in enumerate(_c):\n",
    "    tmp = np.sum(info[i],axis=0).astype(float)\n",
    "    tmp /= np.max(tmp)\n",
    "    plt.bar(range(len(tmp)),tmp,label=i,color=color_pool[_c[i]],bottom=index)\n",
    "#plt.xlim(0,248)\n",
    "plt.ylim(0,3)\n",
    "plt.yticks([],[])\n",
    "#tmp = [1,3,4,5,9,14,47,51,55,56,60,83,84,95,98,123,129,130,138,140,148,149,152,156,158,160,166,178,180,181,183,186,187,208,215]\n",
    "plt.xticks(range(len(targets)),targets,fontsize=8,rotation=-90)\n",
    "plt.legend()\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(modulator_system_path+'Modulator_coprojection_BS.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Thalamus"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "TH_region_list = {'Prethalamus':['RT'],'Dorsal':['EPI'],'Periventricular':['CM','IMD','PVT','PF','RE','Xi','PR','RH','SPFm','SPA','IAD','IAM'],'Intermediate':['PCN','AV','AM','AD','MD','VM','CL','SMT','SPFp','PO','VPL','VPM','VPLpc','VPMpc','Eth','VAL','PIL'],'Superficial':['LD','LP','LGd','GENv','PoT','MG','PP','SGN','POL']}\n",
    "targets = []\n",
    "for i in TH_region_list:\n",
    "    targets += TH_region_list[i]\n",
    "tmp = {'Raphe':['DR'],'SNc':['SNc'],'VTA':['VTA']}\n",
    "info = {}\n",
    "for system in tmp:\n",
    "    info[system] = []\n",
    "    for ii in Scene.scene2List(modulator_system_path+system+'.nv'):\n",
    "        i = ii['sampleid']+ii['name']\n",
    "        info[system].append(get_neuron_axon_info([i],terminal_or_length='terminal',if_combined = True,if_log = False,filter=targets)[1][0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "_c = {'Periventricular':color_pool[3],'Intermediate':color_pool[1],'Superficial':color_pool[0]}\n",
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=False)\n",
    "for i in TH_region_list:\n",
    "    for j in TH_region_list[i]:\n",
    "        neuronvis.addRegion(j,color=_c[i])\n",
    "for k in ['anterior','dorsal','left','right']:\n",
    "    set_view(neuronvis,k)\n",
    "    neuronvis.render.savepng(modulator_system_path+'thalamus_layers_'+k+'.png')\n",
    "neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure(figsize=(15,3))\n",
    "for i in range(1,3):\n",
    "    plt.axhline(i+1,linestyle='--',color='#AAAAAA')\n",
    "_c = {'SNc':3,'VTA':0,'Raphe':5}\n",
    "for index,i in enumerate(_c):\n",
    "    tmp = np.sum(info[i],axis=0).astype(float)\n",
    "    tmp /= np.max(tmp)\n",
    "    plt.bar(range(len(tmp)),tmp,label=i,color=color_pool[_c[i]],bottom=index)\n",
    "#plt.xlim(0,248)\n",
    "plt.ylim(0,3)\n",
    "plt.yticks([],[])\n",
    "#tmp = [1,3,4,5,9,14,47,51,55,56,60,83,84,95,98,123,129,130,138,140,148,149,152,156,158,160,166,178,180,181,183,186,187,208,215]\n",
    "plt.xticks(range(len(targets)),targets,fontsize=8,rotation=-90)\n",
    "plt.legend()\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(modulator_system_path+'Modulator_coprojection.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "masks = {}\n",
    "for i in ['Periventricular','Intermediate','Superficial']:\n",
    "    masks[i] = 0\n",
    "    for j in TH_region_list[i]:\n",
    "        masks[i] = np.where(get_region_mask(j)==1,1,masks[i])\n",
    "info = {}\n",
    "_c = {'SNc':3,'VTA':0,'Raphe':5}\n",
    "for i in ['Periventricular','Intermediate','Superficial']:\n",
    "    info[i] = {}\n",
    "    for system in _c:\n",
    "        if system=='Noradrenergic':\n",
    "            continue\n",
    "        info[i][system] = []\n",
    "        for j in Scene.scene2List(modulator_system_path+system+'.nv'):\n",
    "            for terminal in neuron_terminals[j['sampleid']+j['name']]['terminals']:\n",
    "                if if_inside_mask(masks[i],np.array(terminal)/10):\n",
    "                    info[i][system].append(terminal.copy())\n",
    "for i in info:\n",
    "    m = get_contour(np.sum(masks[i],axis=-1).T>0)\n",
    "    plt.imshow(m,cmap='Greys')\n",
    "    for j in info[i]:\n",
    "        x = np.array(info[i][j])\n",
    "        plt.scatter(x[:,0]/10,x[:,1]/10,color=color_pool[_c[j]],s=1)\n",
    "    plt.rcParams['pdf.fonttype'] = 42\n",
    "    plt.rcParams['ps.fonttype'] = 42\n",
    "    plt.xlim(400,1000)\n",
    "    plt.ylim(600,200)\n",
    "    plt.axis('off')\n",
    "    plt.savefig(modulator_system_path+'Modulator_TH'+i+'.pdf',format='pdf',bbox_inches='tight')\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### HY"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "HY_region_list = {'HY':brainstem_region_hierachy['HY']}\n",
    "targets = []\n",
    "for i in HY_region_list:\n",
    "    targets += HY_region_list[i]\n",
    "tmp = {'Raphe':['DR'],'SNc':['SNc'],'VTA':['VTA']}\n",
    "info = {}\n",
    "for system in tmp:\n",
    "    info[system] = []\n",
    "    for ii in Scene.scene2List(modulator_system_path+system+'.nv'):\n",
    "        i = ii['sampleid']+ii['name']\n",
    "        info[system].append(get_neuron_axon_info([i],terminal_or_length='terminal',if_combined = True,if_log = False,filter=targets)[1][0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure(figsize=(15,3))\n",
    "for i in range(1,3):\n",
    "    plt.axhline(i+1,linestyle='--',color='#AAAAAA')\n",
    "_c = {'SNc':3,'VTA':0,'Raphe':5}\n",
    "for index,i in enumerate(_c):\n",
    "    tmp = np.sum(info[i],axis=0).astype(float)\n",
    "    tmp /= np.max(tmp)\n",
    "    plt.bar(range(len(tmp)),tmp,label=i,color=color_pool[_c[i]],bottom=index)\n",
    "#plt.xlim(0,248)\n",
    "plt.ylim(0,3)\n",
    "plt.yticks([],[])\n",
    "#tmp = [1,3,4,5,9,14,47,51,55,56,60,83,84,95,98,123,129,130,138,140,148,149,152,156,158,160,166,178,180,181,183,186,187,208,215]\n",
    "plt.xticks(range(len(targets)),targets,fontsize=8,rotation=-90)\n",
    "plt.legend()\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(modulator_system_path+'Modulator_coprojection_HY.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "masks = {'LHA':get_region_mask('LHA')}\n",
    "info = {}\n",
    "_c = {'SNc':3,'VTA':0,'Raphe':5}\n",
    "for i in ['LHA']:\n",
    "    info[i] = {}\n",
    "    for system in _c:\n",
    "        if system=='Noradrenergic':\n",
    "            continue\n",
    "        info[i][system] = []\n",
    "        for j in Scene.scene2List(modulator_system_path+system+'.nv'):\n",
    "            for terminal in neuron_terminals[j['sampleid']+j['name']]['terminals']:\n",
    "                if if_inside_mask(masks[i],np.array(terminal)/10):\n",
    "                    info[i][system].append(terminal.copy())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in info:\n",
    "    m = get_contour(np.sum(masks[i],axis=1)>0)\n",
    "    plt.imshow(m,cmap='Greys')\n",
    "    for j in info[i]:\n",
    "        x = np.array(info[i][j])\n",
    "        plt.scatter(x[:,2]/10,x[:,0]/10,color=color_pool[_c[j]],s=2)\n",
    "    plt.rcParams['pdf.fonttype'] = 42\n",
    "    plt.rcParams['ps.fonttype'] = 42\n",
    "    plt.xlim(575-300,575+300)\n",
    "    plt.ylim(900,500)\n",
    "    #plt.axis('off')\n",
    "    plt.savefig(modulator_system_path+'Modulator_HY'+i+'.pdf',format='pdf',bbox_inches='tight')\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for j in info[i]:\n",
    "    x = np.array(info[i][j])\n",
    "    a,b = estimate_density_function(x[:,0]/10,parameter=[500,400,1,25],weight=[])\n",
    "    plt.plot(b,color=color_pool[_c[j]])\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(modulator_system_path+'Modulator_LHA_distribution.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()\n",
    "np.round(stats.mannwhitneyu(info['LHA']['VTA'],info['LHA']['Raphe'])[1],4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "masks = {'Test':get_region_mask('MD')+get_region_mask('VM')+get_region_mask('LHA')}\n",
    "info = {}\n",
    "_c = {'SNc':3,'VTA':0,'Raphe':5}\n",
    "for i in ['Test']:\n",
    "    info[i] = {}\n",
    "    for system in _c:\n",
    "        if system=='Noradrenergic':\n",
    "            continue\n",
    "        info[i][system] = []\n",
    "        for j in Scene.scene2List(modulator_system_path+system+'.nv'):\n",
    "            for terminal in neuron_terminals[j['sampleid']+j['name']]['terminals']:\n",
    "                if if_inside_mask(masks[i],np.array(terminal)/10):\n",
    "                    info[i][system].append(terminal.copy())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=False)\n",
    "for i in _c:\n",
    "    draw_points(neuronvis,info['Test'][i],[color_pool[_c[i]].tolist() for j in info['Test'][i]])\n",
    "for i in ['MD','VM','LHA']:\n",
    "    neuronvis.addRegion(i,color=color_pool[1])\n",
    "neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### CNU"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "CNU_region_list = {'STR':['STRd','ACB','FS','OT','LSX','AAA','CEA','MEA'],'PAL':['PALd','PALv','PALm','PALc']}\n",
    "targets = []\n",
    "for i in CNU_region_list:\n",
    "    targets += CNU_region_list[i]\n",
    "tmp = {'Raphe':['DR'],'SNc':['SNc'],'VTA':['VTA']}\n",
    "info = {}\n",
    "for system in tmp:\n",
    "    info[system] = []\n",
    "    for ii in Scene.scene2List(modulator_system_path+system+'.nv'):\n",
    "        i = ii['sampleid']+ii['name']\n",
    "        a = get_neuron_axon_info([i],terminal_or_length='terminal',if_combined = True,if_log = False,filter=targets)[1][0].tolist()\n",
    "        a[5] = np.sum(a[5:8])\n",
    "        info[system].append(a[:6]+a[8:])\n",
    "CNU_region_list = {'STR':['STRd','ACB','FS','OT','LSX','sAMY'],'PAL':['PALd','PALv','PALm','PALc']}\n",
    "targets = []\n",
    "for i in CNU_region_list:\n",
    "    targets += CNU_region_list[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure(figsize=(15,3))\n",
    "for i in range(1,3):\n",
    "    plt.axhline(i+1,linestyle='--',color='#AAAAAA')\n",
    "_c = {'SNc':3,'VTA':0,'Raphe':5}\n",
    "for index,i in enumerate(_c):\n",
    "    tmp = np.sum(info[i],axis=0).astype(float)\n",
    "    tmp /= np.max(tmp)\n",
    "    plt.bar(range(len(tmp)),tmp,label=i,color=color_pool[_c[i]],bottom=index)\n",
    "#plt.xlim(0,248)\n",
    "plt.ylim(0,3)\n",
    "plt.yticks([],[])\n",
    "#tmp = [1,3,4,5,9,14,47,51,55,56,60,83,84,95,98,123,129,130,138,140,148,149,152,156,158,160,166,178,180,181,183,186,187,208,215]\n",
    "plt.xticks(range(len(targets)),targets,fontsize=8,rotation=-90)\n",
    "plt.legend()\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(modulator_system_path+'Modulator_coprojection_CNU.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Recurrent system"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "recurrent_system_path = data_path+'recurrent_system/'\n",
    "create_dir(recurrent_system_path)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## loss accuracy compare"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rt_loss = np.load('rt_loss.npy')\n",
    "rt_history = np.load('rt_history.npy')\n",
    "nrt_loss = np.load('nrt_loss.npy')\n",
    "nrt_history = np.load('nrt_history.npy')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure(figsize=(8,4))\n",
    "ax = fig.add_subplot(2,1,1)\n",
    "ax.plot(rt_loss,label='recurrent',color=color_pool[0])\n",
    "ax.plot(nrt_loss,label='non-recurrent',color=color_pool[3])\n",
    "ax.set_title('Train loss curve')\n",
    "ax = fig.add_subplot(2,1,2)\n",
    "ax.plot(rt_history,label='recurrent',color=color_pool[0])\n",
    "ax.plot(nrt_history,label='non-recurrent',color=color_pool[3])\n",
    "ax.set_title('Train accuracy curve')\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(recurrent_system_path+'recurrent_loss_accuracy_system.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Brainstem detail"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "brainstem_path = data_path+'brainstem/'\n",
    "create_dir(brainstem_path)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Medulla"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "medulla_path = brainstem_path+'medulla/'\n",
    "create_dir(medulla_path)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### region visualization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "BS = ['MB','P','MY']\n",
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "for i in [['SPVO','SPVI','SPVC'],['IRN','GRN','MARN','PGRN','MDRN','LRN','LIN']]:\n",
    "    neuronvis.clear(root=True)\n",
    "    for j in BS:\n",
    "        neuronvis.addRegion(j)\n",
    "    for j in i:\n",
    "        neuronvis.addRegion(j,[1,0,0])\n",
    "    set_view(neuronvis,'left')\n",
    "    neuronvis.render.savepng(medulla_path+'region_'+'_'.join(i)+'.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### axon"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if not os.path.exists(medulla_path+'projection_axon_info.npy'):\n",
    "    mask = get_region_mask('MY')\n",
    "    import random\n",
    "    _max = 200\n",
    "    color_transform = {'VIS':color_pool[3],'ACA':color_pool[4],'MOs':color_pool[0],'SSs':color_pool[1],'AUD':color_pool[2],'AI':color_pool[5],'SSp':color_pool[1],'MOp':color_pool[3],'TEa':color_pool[5],'PL':color_pool[7],'ORB':color_pool[6],'ILA':color_pool[8],'RSP':color_pool[9],}\n",
    "    info = {}\n",
    "    for neuron in tool_bar(cortex_neuron_name,desc='iteration on neurons (generating mapping colors) ...',leave=False,colour='#55AF7B'):\n",
    "        region = neuron_to_brainstem_information[neuron]['cortex_region']\n",
    "        if region not in info:\n",
    "            info[region] = {'neuron':[]}\n",
    "        info[region]['neuron'].append(neuron)\n",
    "    for region in info:\n",
    "        if region not in color_transform:\n",
    "            continue\n",
    "        info[region]['axon'] = get_terminal_lines_in_mask(info[region]['neuron'] if len(info[region]['neuron'])<=_max else random.sample(info[region]['neuron'],_max),mask,color=color_transform[region])\n",
    "    np.save(medulla_path+'projection_axon_info.npy',info)\n",
    "else:\n",
    "    info = np.load(medulla_path+'projection_axon_info.npy',allow_pickle=True).item()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = get_neuron_axon_info(cortex_neuron_name,terminal_or_length='terminal',if_combined=True,if_log=False,filter=brainstem_region_hierachy['MY']+brainstem_region_hierachy['SC'])[1]\n",
    "tmp = {}\n",
    "for i,j in zip(cortex_neuron_name,X):\n",
    "    region = neuron_to_brainstem_information[i]['cortex_region']\n",
    "    if region not in tmp:\n",
    "        tmp[region] = []\n",
    "    tmp[region].append([np.sum(j[:-1]),j[-1]])\n",
    "x = np.array([np.mean(tmp[i],axis=0) for i in tmp])\n",
    "a = [[i[0],j] for i,j in zip(x,tmp)]\n",
    "sorted_a = sorted(a, key=lambda x: x[0],reverse=True)\n",
    "v = [i[0] for i in sorted_a]\n",
    "x_label = [i[1] for i in sorted_a]\n",
    "plt.bar(range(len(tmp)),v,color=color_pool[0])\n",
    "_ = plt.xticks(range(len(x_label)),x_label,rotation=-45,fontsize=fontsize_ticks)\n",
    "plt.title('Correlation',fontsize=fontsize_title)\n",
    "plt.xlabel('Source',fontsize=fontsize_label)\n",
    "plt.ylabel('Average terminal',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.title('Medulla projection',fontsize=fontsize_title)\n",
    "plt.savefig(medulla_path+'medulla_projection_from_cortex_subareas.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()\n",
    "a = [[i[1],j] for i,j in zip(x,tmp)]\n",
    "sorted_a = sorted(a, key=lambda x: x[0],reverse=True)\n",
    "v = [i[0] for i in sorted_a]\n",
    "x_label = [i[1] for i in sorted_a]\n",
    "plt.bar(range(len(tmp)),v,color=color_pool[3])\n",
    "_ = plt.xticks(range(len(x_label)),x_label,rotation=-45,fontsize=fontsize_ticks)\n",
    "plt.title('Correlation',fontsize=fontsize_title)\n",
    "plt.xlabel('Source',fontsize=fontsize_label)\n",
    "plt.ylabel('Average terminal',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.title('Spine projection',fontsize=fontsize_title)\n",
    "plt.savefig(medulla_path+'spine_projection_from_cortex_subareas.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### sensory medulla compare"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "targets = ['DCN','VNC','CN','SPVO','SPVI','SPVC','NTS']\n",
    "X = 0\n",
    "for i in neuron_to_brainstem_information:\n",
    "    X += np.array(get_axonal_statics_by_regions(i,targets,'terminalregion'))\n",
    "X[0],X[1],X[2],np.sum(X[3:6]),X[6]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Insula and NTS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "for i,j in zip([4,5],[0,3]):\n",
    "    for k in Scene.scene2List(data_path+'../project_cortex_to_brainstem_data_v3/cortical_part/insular/cluster_'+str(i)+'.nv'):\n",
    "        neuronvis.addNeuronByID(k['sampleid'],k['name'],color_pool[j],mirrorToRight=True,somaHide=False,dendriteHide=True)\n",
    "for j in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "    set_view(neuronvis,j)\n",
    "    neuronvis.render.savepng(medulla_path+'NTS_projection_'+j+'.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "for i,j in zip(['220198127.swc','220196126.swc'],[0,3]):\n",
    "    neuronvis.clear()\n",
    "    neuronvis.addNeuronByID(i[:-7],i[-7:],color_pool[j],mirrorToRight=True,somaHide=False,dendriteHide=True,isLine=False)\n",
    "    for j in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "        set_view(neuronvis,j)\n",
    "        neuronvis.render.savepng(medulla_path+'NTS_projection_'+i[:-7]+'_'+j+'.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### NTS projection comparison"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = ['NTS']\n",
    "mask = 0.0\n",
    "for i,j in enumerate(target):\n",
    "    mask = np.where(get_region_mask(j),i+1,mask)\n",
    "X = {}\n",
    "for id,neuron in enumerate(neuron_to_brainstem_information):\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']\n",
    "    if region not in ['AI','GU','VISC']:\n",
    "        continue\n",
    "    if region not in X:\n",
    "        X[region] = {'terminal':[]}\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            X[region]['terminal'].append(terminal.copy())\n",
    "            if neuron_terminals[neuron]['mirror_mark']:\n",
    "                X[region]['terminal'][-1][2] = 11400-X[region]['terminal'][-1][2]\n",
    "mask_2D = np.sum(mask,axis=1)\n",
    "a,b = get_value_range(mask_2D)\n",
    "tmp = [np.mean(a),np.mean(b)]\n",
    "tmp2 = np.max([a[1]-a[0],b[1]-b[0]])\n",
    "_r = [tmp[0]-tmp2/2,tmp[0]+tmp2/2,tmp[1]+tmp2/2,tmp[1]-tmp2/2]\n",
    "color_transform = {'AI':0,'GU':1,'VISC':3}\n",
    "m = 1-get_contour(mask_2D>0)\n",
    "fig,ax = plt.subplots(figsize=(5,5))\n",
    "ax.matshow(m,cmap='gray')\n",
    "for i in X:\n",
    "    tmp = np.array(X[i]['terminal'])\n",
    "    ax.scatter(tmp[:,2]/10,tmp[:,0]/10,color=color_pool[color_transform[i]],s=1,alpha=0.5)\n",
    "ax.set_xlim(_r[0],_r[1])\n",
    "ax.set_ylim(_r[2],_r[3])\n",
    "ax.set_title(\"Terminal in NTS\",fontsize=fontsize_title)\n",
    "ax.set_axis_off()\n",
    "ax.legend(handles=[mpatches.Patch(color=color_pool[color_transform[i]], label=i) for i in color_transform],loc='lower right')\n",
    "plt.savefig(medulla_path+'NTS_terminal.pdf',format='pdf',bbox_inches='tight',dpi=100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### NTS average terminal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = {}\n",
    "for neuron in neuron_to_brainstem_information:\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']\n",
    "    if region not in ['AI','GU','VISC']:\n",
    "        continue\n",
    "    if region not in X:\n",
    "        X[region] = []\n",
    "    X[region].append(neuron_to_brainstem_information[neuron]['axonal_projection_terminal_combined'][brainstem_region.index('NTS')])\n",
    "plt.figure(figsize=(4,5))\n",
    "plt.bar(range(len(X)),[np.mean(X[i]) for i in X],width=0.25,color=color_pool[0])\n",
    "plt.xticks(range(len(X)),[i+'(n='+str(len(X[i]))+')' for i in X],fontsize=fontsize_ticks)\n",
    "plt.xlim(-0.5,len(X)-0.5)\n",
    "plt.xlabel('Source',fontsize=fontsize_label)\n",
    "plt.ylabel('Average terminal number',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.title(\"NTS terminal statics\",fontsize=fontsize_title)\n",
    "plt.savefig(medulla_path+'NTS_statics.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### NTS bilateral"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "source = ['AI','GU','VISC']\n",
    "target = ['NTS']\n",
    "mask = 0.0\n",
    "for i,j in enumerate(target):\n",
    "    mask = np.where(get_region_mask(j),i+1,mask)\n",
    "X = {}\n",
    "for id,neuron in enumerate(neuron_to_brainstem_information):\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']\n",
    "    if region not in source:\n",
    "        continue\n",
    "    if region not in X:\n",
    "        X[region] = {i:[] for i in target}\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        v = int(get_mask_value(mask,np.array(terminal)/10))\n",
    "        if v>0:\n",
    "            X[region][target[v-1]].append(terminal.copy())\n",
    "            if neuron_terminals[neuron]['mirror_mark']:\n",
    "                X[region][target[v-1]][-1][2] = 11400-X[region][target[v-1]][-1][2]\n",
    "tmp = {}\n",
    "for i in source:\n",
    "    tmp[i] = []\n",
    "    for j in X[i]:\n",
    "        tmp[i] += X[i][j]\n",
    "fig = plt.figure(figsize=(5,5))\n",
    "ax = fig.add_subplot(1,1,1)\n",
    "parts = ax.violinplot([np.array(tmp[i])[:,2] for i in tmp],showmeans=False,showextrema=False,showmedians=False,vert=False)\n",
    "for pc,c in zip(parts['bodies'],[0,1,3]):\n",
    "    pc.set_facecolor(color_pool[c])\n",
    "    pc.set_edgecolor('black')\n",
    "    pc.set_alpha(1)\n",
    "ax.axvline(5700,color='k',linestyle='--')\n",
    "ax.invert_yaxis()\n",
    "ax.set_xticks([5700-1500,5700,5700+1500],['ipsilateral\\n(4200)','midline\\n(5700)','contralatel\\n(7200)'],fontsize=fontsize_ticks)\n",
    "ax.set_yticks([1,2,3],source,fontsize=fontsize_ticks)\n",
    "ax.set_xlabel('Distance from lateral (\\u00B5m)',fontsize=fontsize_label)\n",
    "ax.set_ylabel('Target',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "ax.set_title('Terminal distribution in NTS',fontsize=fontsize_title)\n",
    "color_transform = {'AI':0,'GU':1,'VISC':3}\n",
    "ax.legend(handles=[mpatches.Patch(color=color_pool[color_transform[i]], label=i) for i in color_transform],loc='upper right')\n",
    "plt.savefig(medulla_path+'NTS_bilateral_statistics.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### SSp head and body project down to spine"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = {}\n",
    "for neuron in neuron_to_brainstem_information:\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']\n",
    "    if region not in ['SSp']:\n",
    "        continue\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_subregion'][1:]\n",
    "    if region not in X:\n",
    "        X[region] = []\n",
    "    X[region].append([np.sum(neuron_to_brainstem_information[neuron]['axonal_projection_terminal_combined'][brainstem_region.index('SPVO'):brainstem_region.index('SPVO')+3]),neuron_to_brainstem_information[neuron]['axonal_projection_terminal_combined'][-1]])\n",
    "width = 0.25\n",
    "scale = 1.1\n",
    "plt.figure(figsize=(7,5))\n",
    "plt.bar(range(len(X)),[np.mean(X[i],axis=0)[0] for i in X],width=width,color=color_pool[0],label='SPV')\n",
    "plt.bar([i+width*scale for i in range(len(X))],[np.mean(X[i],axis=0)[1] for i in X],width=width,color=color_pool[3],label='Spine')\n",
    "plt.xticks(range(len(X)),[i+'\\n(n='+str(len(X[i]))+')' for i in X],fontsize=fontsize_ticks)\n",
    "plt.xlim(-0.5,len(X)-0.5)\n",
    "plt.xlabel('Source',fontsize=fontsize_label)\n",
    "plt.ylabel('Average terminal number',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.title(\"SPV and Spine terminal statics\",fontsize=fontsize_title)\n",
    "plt.legend()\n",
    "plt.savefig(medulla_path+'SSp_spine_statics.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=False)\n",
    "for neuron in neuron_to_brainstem_information:\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']\n",
    "    if region not in ['SSp']:\n",
    "        continue\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_subregion'][1:]\n",
    "    if region in ['bfd','n','m']:\n",
    "        color = color_pool[0].tolist()\n",
    "    else:\n",
    "        color = color_pool[3].tolist()\n",
    "    neuron_tree = iondata.getNeuronTreeByID(neuron[:-7],neuron[-7:])\n",
    "    neuron_tree.somaRadius = 10\n",
    "    neuronvis.addNeuronTree(neuron_tree,axonColor=color,somaColor=[0.5,0.5,0.5],dendriteColor=[0.5,0.5,0.5],mirrorToRight=True,isLine=True,dendriteHide=True)\n",
    "neuronvis.render.savepng(medulla_path+'SSp_axon_head_body_default.png')\n",
    "for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "    set_view(neuronvis,i)\n",
    "    neuronvis.render.savepng(medulla_path+'SSp_axon_head_body_'+i+'.png')\n",
    "neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### SS and MO difference"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sensory_neuron_list = []\n",
    "motor_neuron_list = []\n",
    "target = ['PSV','SPVO','SPVI','SPVC','PARN','IRN','GRN','MARN','PGRNl','PGRNd','MDRNd','MDRNv','LRNp','LRNm','LIN','V','VII','DMX','AMB']\n",
    "for i in neuron_to_brainstem_information:\n",
    "    if neuron_to_brainstem_information[i]['cortex_region'] in ['SSp','SSs']:\n",
    "        sensory_neuron_list.append(i)\n",
    "for i in neuron_to_brainstem_information:\n",
    "    if neuron_to_brainstem_information[i]['cortex_region'] in ['MOs','MOp']:\n",
    "        motor_neuron_list.append(i)\n",
    "SS_neuron_info = get_neuron_axon_info(sensory_neuron_list,terminal_or_length='terminal',if_combined=True,if_log=False,filter=target)\n",
    "MO_neuron_info = get_neuron_axon_info(motor_neuron_list,terminal_or_length='terminal',if_combined=True,if_log=False,filter=target)\n",
    "width = 0.2\n",
    "plt.bar([0,1],[np.sum(np.array(SS_neuron_info[1])[:,:4])/len(SS_neuron_info[1]),np.sum(np.array(SS_neuron_info[1])[:,4:])/len(SS_neuron_info[1])],label='SS',width=width*0.75,color=color_pool[3])\n",
    "plt.bar(np.array([0,1])+width,[np.sum(np.array(MO_neuron_info[1])[:,:4])/len(MO_neuron_info[1]),np.sum(np.array(MO_neuron_info[1])[:,4:])/len(MO_neuron_info[1])],label='MO',width=width*0.75,color=color_pool[0])\n",
    "plot_post_process(title='The terminal distribution in medulla',xlabel='Modalities',ylabel='Average terminal number',save_path=medulla_path+'MY_S_M')\n",
    "plt.xticks(np.array([0,1])+width/2,['Sensory part','Motor part'],fontsize=fontsize_ticks)\n",
    "plt.xticks(rotation = 0)\n",
    "plt.legend()\n",
    "plt.savefig(medulla_path+'SS_MO_difference.pdf',format='pdf')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "[np.sum(np.array(SS_neuron_info[1])[:,:4])/len(SS_neuron_info[1]),np.sum(np.array(SS_neuron_info[1])[:,4:])/len(SS_neuron_info[1])],[np.sum(np.array(MO_neuron_info[1])[:,:4])/len(MO_neuron_info[1]),np.sum(np.array(MO_neuron_info[1])[:,4:])/len(MO_neuron_info[1])]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### neuron examples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear()\n",
    "for i in ['PRNr','PRNc']:\n",
    "    neuronvis.addRegion(i)\n",
    "for i,j in zip(['221085133.swc','221481094.swc'],[0,1]):\n",
    "    neuron_tree = iondata.getNeuronTreeByID(i[:-7],i[-7:])\n",
    "    neuron_tree.somaRadius = 25\n",
    "    neuronvis.addNeuronTree(neuron_tree,axonColor=color_pool[j],somaColor=[0.5,0.5,0.5],dendriteColor=[0.5,0.5,0.5],mirrorToRight=True,isLine=False)\n",
    "set_view(neuronvis,'dorsal')\n",
    "neuronvis.render.savepng(medulla_path+'vertical_horizontal_branch_example.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### SS and MO form a unitary mapping"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sensory_neuron_list = {}\n",
    "for i in Scene.scene2List(medulla_path+'trigeminal_nerve_MO_SS_topology_line.nv'):\n",
    "    g = i['color']['r']\n",
    "    if g not in sensory_neuron_list:\n",
    "        sensory_neuron_list[g] = []\n",
    "    sensory_neuron_list[g].append(i['sampleid']+i['name'])\n",
    "mask = 0.0\n",
    "for i in ['PSV','SPVO','SPVI','SPVC']:\n",
    "    mask += get_region_mask(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sensory_neuron_list = {0:[],1:[],2:[]}\n",
    "result = _flatmap*1\n",
    "for neuron in neuron_to_brainstem_information:\n",
    "    if neuron_to_brainstem_information[neuron]['cortex_region'] not in ['SSp']:\n",
    "        continue\n",
    "    l = neuron_to_brainstem_information[neuron]['soma_flatten_position']\n",
    "    k = -0.06*l[0]-0.15*l[1]\n",
    "    if k>-130:\n",
    "        c = color_pool[1]\n",
    "        sensory_neuron_list[2].append(neuron)\n",
    "    elif k>-150:\n",
    "        c = color_pool[0]\n",
    "        sensory_neuron_list[1].append(neuron)\n",
    "    else:\n",
    "        c = color_pool[3]\n",
    "        sensory_neuron_list[0].append(neuron)\n",
    "    cv.circle(result,[int(l[0])*2, int(l[1])*2],15,np.array(c)*255,-1)\n",
    "for neuron in neuron_to_brainstem_information:\n",
    "    if neuron_to_brainstem_information[neuron]['cortex_region'] not in ['SSs']:\n",
    "        continue\n",
    "    l = neuron_to_brainstem_information[neuron]['soma_flatten_position']\n",
    "    k = 0.2*l[0]-l[1]\n",
    "    if k>-530:\n",
    "        c = color_pool[1]\n",
    "        sensory_neuron_list[2].append(neuron)\n",
    "    elif k>-630:\n",
    "        c = color_pool[0]\n",
    "        sensory_neuron_list[1].append(neuron)\n",
    "    else:\n",
    "        c = color_pool[3]\n",
    "        sensory_neuron_list[0].append(neuron)\n",
    "    cv.circle(result,[int(l[0])*2, int(l[1])*2],15,np.array(c)*255,-1)\n",
    "for neuron in neuron_to_brainstem_information:\n",
    "    if neuron_to_brainstem_information[neuron]['cortex_region'] not in ['MOp','MOs']:\n",
    "        continue\n",
    "    l = neuron_to_brainstem_information[neuron]['soma_flatten_position']\n",
    "    k = l[0]+0.2*l[1]\n",
    "    if k>970:\n",
    "        c = color_pool[3]\n",
    "        sensory_neuron_list[0].append(neuron)\n",
    "    elif k>870:\n",
    "        c = color_pool[0]\n",
    "        sensory_neuron_list[1].append(neuron)\n",
    "    else:\n",
    "        c = color_pool[1]\n",
    "        sensory_neuron_list[2].append(neuron)\n",
    "    cv.circle(result,[int(l[0])*2, int(l[1])*2],15,np.array(c)*255,-1)\n",
    "fig,ax = plt.subplots(figsize=(5,5))\n",
    "ax.imshow(result,zorder=0)\n",
    "ax.imshow(flatmap_mask,zorder=1)\n",
    "ax.imshow(flatmap,zorder=2)\n",
    "ax.set_xlim(flatten_map_r[0],flatten_map_r[1])\n",
    "ax.set_ylim(flatten_map_r[2],flatten_map_r[3])\n",
    "ax.set_axis_off()\n",
    "plt.title('Soma distribution',fontsize=fontsize_title)\n",
    "plt.savefig(medulla_path+'soma_distribution.pdf',format='pdf')\n",
    "plt.show()\n",
    "g_c = {0:3,1:0,2:1}\n",
    "g_l = {0:'bfd',1:'n',2:'m'}\n",
    "X = {}\n",
    "Y = {}\n",
    "for g in sensory_neuron_list:\n",
    "    X[g] = []\n",
    "    Y[g] = []\n",
    "    for neuron in sensory_neuron_list[g]:\n",
    "        for terminal in neuron_terminals[neuron]['terminals']:\n",
    "            if if_inside_mask(mask,np.array(terminal)/10):\n",
    "                X[g].append(terminal.copy())\n",
    "                if X[g][-1][2]>5700:\n",
    "                    X[g][-1][2] = 11400-X[g][-1][2]\n",
    "                Y[g].append(color_pool[g_c[g]])\n",
    "from mpl_toolkits.axes_grid1 import make_axes_locatable\n",
    "fig, ax = plt.subplots(figsize=(6.5+0.5, 6.5))\n",
    "ax_histy = make_axes_locatable(ax).append_axes(\"right\", 0.5, pad=0.1, sharey=ax)\n",
    "ax.imshow(np.sum(mask,axis=-1).T>0,cmap='gray')\n",
    "A = np.array([i for i in range(mask.shape[1])])\n",
    "for i,g in enumerate(X):\n",
    "    tmp = np.array(X[g])\n",
    "    ax.scatter(tmp[:,0]/10,tmp[:,1]/10,s=1,label=g_l[g],c=Y[g])\n",
    "    tmp2 = 0.0\n",
    "    for k in tmp[:,1]:\n",
    "        tmp2 += func_gaussian(A,k/10,7)\n",
    "    ax_histy.plot(tmp2/np.sum(tmp2),A,label=g_l[g],c=color_pool[g_c[g]])\n",
    "z = np.sum(mask,axis=-1)\n",
    "x = (np.sum(z,axis=-1)>0).tolist()\n",
    "ax.set_xlim(x.index(1),len(x)-1-x[::-1].index(1))\n",
    "x = (np.sum(z,axis=0)>0).tolist()\n",
    "ax.set_ylim(len(x)-1-x[::-1].index(1),x.index(1))\n",
    "plt.legend()\n",
    "ax.set_axis_off()\n",
    "ax.set_aspect('equal', adjustable='box')\n",
    "_ = ax_histy.set_yticks([],[])\n",
    "plt.savefig(medulla_path+'terminal_distribution.pdf',format='pdf')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = ['PSV','SPVO','SPVI','SPVC']\n",
    "tmp = {}\n",
    "for i in neuron_to_brainstem_information:\n",
    "    if neuron_to_brainstem_information[i]['cortex_region'] not in ['MOs','MOp','SSp','SSs']:\n",
    "        continue\n",
    "    if neuron_to_brainstem_information[i]['cortex_region'] not in tmp:\n",
    "        tmp[neuron_to_brainstem_information[i]['cortex_region']] = []\n",
    "    tmp[neuron_to_brainstem_information[i]['cortex_region']].append(i)\n",
    "for i in tmp:\n",
    "    tmp[i] = get_neuron_axon_info(tmp[i],terminal_or_length='terminal',if_combined=True,if_log=False,filter=target)[1]\n",
    "th = 0\n",
    "for i,j in enumerate(['MOs','MOp','SSp','SSs']):\n",
    "    plt.plot(range(len(target)),np.mean(np.array(tmp[j])[np.sum(tmp[j],axis=-1)>th],axis=0)/np.sum(np.mean(np.array(tmp[j])[np.sum(tmp[j],axis=-1)>th],axis=0)),label=j,marker='o')\n",
    "plt.legend()\n",
    "plt.ylim(0,0.5)\n",
    "plot_post_process(title='The terminal distribution in trigemninal nuclei',xlabel='Segemnts',ylabel='Ratio (%)',save_path=medulla_path+'line_difference')\n",
    "plt.xticks(range(len(target)),target,fontsize=fontsize_ticks)\n",
    "plt.xticks(rotation = 0)\n",
    "plt.savefig(medulla_path+'line_difference.pdf',format='pdf')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### percent ratio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp2 = {'SSp':[0,0],'SSs':[0,0],'MOp':[0,0],'MOs':[0,0]}\n",
    "for n,k in zip(neuron_to_brainstem_information,get_neuron_axon_info(cortex_neuron_name,terminal_or_length='terminal',if_combined=True,if_log=False,filter=['PSV','SPVO','SPVI','SPVC'])[1]):\n",
    "        r = neuron_to_brainstem_information[n]['cortex_region']\n",
    "        if r in tmp2:\n",
    "                if np.sum(k)>5:\n",
    "                        tmp2[r][0] += 1\n",
    "                tmp2[r][1] += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in tmp2:\n",
    "    print(tmp2[i][0]/tmp2[i][1],tmp2[i][0],tmp2[i][1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_labels = list(tmp2.keys())\n",
    "tmp = []\n",
    "for i in tmp2:\n",
    "    tmp.append(tmp2[i][0]/tmp2[i][1]*100)\n",
    "h = 0.5\n",
    "fig = plt.figure(figsize=(3,6))\n",
    "plt.barh(range(len(x_labels)),tmp,color=color_pool[0],height=h)\n",
    "plt.yticks(range(len(x_labels)),x_labels,fontsize=fontsize_ticks)\n",
    "plt.xlabel('Ratio (%)',fontsize=fontsize_label)\n",
    "plt.ylabel('Source',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.title('Neuron ratio',fontsize=fontsize_title)\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(medulla_path+'Hallucination_neuron_ratio.pdf',format='pdf')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### MO can target basal plate to influence motor output directly"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = ['PARN','IRN','GRN','MARN','PGRNl','PGRNd','MDRNd','MDRNv','LRNp','LRNm','LIN','V','VII','DMX','AMB']\n",
    "MO_neuron_info = get_neuron_axon_info(motor_neuron_list,terminal_or_length='terminal',if_combined=True,if_log=False,filter=target)\n",
    "X = [np.sum(MO_neuron_info[1][:,:-4]),np.sum(MO_neuron_info[1][:,-4:])]\n",
    "def func(pct, allvals):\n",
    "    absolute = int(np.round(pct/100.*np.sum(allvals)))\n",
    "    return f\"{pct:.1f}%\\n({absolute:d})\"\n",
    "plt.pie(X,colors=[color_pool[0],color_pool[3]],labels=['reticular formation','motor nuclei'], autopct=lambda pct: func(pct,X),textprops=dict(color=\"w\"))\n",
    "plt.legend()\n",
    "plt.savefig(medulla_path+'MO_RF_M_distribution.pdf',format='pdf')\n",
    "plt.show()\n",
    "MOp_neuron_list = []\n",
    "MOs_neuron_list = []\n",
    "for neuron in motor_neuron_list:\n",
    "    if neuron_to_brainstem_information[neuron]['cortex_region']=='MOp':\n",
    "        MOp_neuron_list.append(neuron)\n",
    "    else:\n",
    "        MOs_neuron_list.append(neuron)\n",
    "MOp_neuron_info = get_neuron_axon_info(MOp_neuron_list,terminal_or_length='terminal',if_combined=True,if_log=False,filter=target)\n",
    "MOs_neuron_info = get_neuron_axon_info(MOs_neuron_list,terminal_or_length='terminal',if_combined=True,if_log=False,filter=target)\n",
    "width = 0.2\n",
    "plt.bar([0,1],[np.sum(np.array(MOp_neuron_info[1])[:,:-4])/len(MOp_neuron_info[1]),np.sum(np.array(MOp_neuron_info[1])[:,-4:])/len(MOp_neuron_info[1])],label='MOp',width=width*0.75,color=color_pool[3])\n",
    "plt.bar(np.array([0,1])+width,[np.sum(np.array(MOs_neuron_info[1])[:,:-4])/len(MOs_neuron_info[1]),np.sum(np.array(MOs_neuron_info[1])[:,-4:])/len(MOs_neuron_info[1])],label='MOs',width=width*0.75,color=color_pool[0])\n",
    "plot_post_process(title='Reticular formation and motor nuclei',xlabel='Modalities',ylabel='Average terminal number',save_path=medulla_path+'MOp_MOs_RF_distribution')\n",
    "plt.xticks(np.array([0,1])+width/2,['reticular formation','motor nuceli'],fontsize=fontsize_ticks)\n",
    "plt.xticks(rotation = 0)\n",
    "plt.legend()\n",
    "plt.savefig(medulla_path+'MOp_MOs_RF_distribution.pdf',format='pdf')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### RF come mainly from MO"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = ['IRN','GRN','MARN','PGRNd','PGRNl','MDRNd','MDRNv','LRNm','LRNp','LIN']\n",
    "info = {}\n",
    "for i in neuron_to_brainstem_information:\n",
    "    d = neuron_to_brainstem_information[i]['soma_depth']\n",
    "    p = neuron_to_brainstem_information[i]['soma_flatten_position']\n",
    "    info[i] = [d,p]\n",
    "    tmp = 0\n",
    "    for j in target:\n",
    "        tmp += neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][brainstem_region.index(j)]\n",
    "    info[i].append(tmp)\n",
    "r = 200\n",
    "s = 125\n",
    "transition_color = np.array([[255,255,255],[161,238,204],[255,242,193],[246,49,141]])/255.0\n",
    "cmap1 = mcolors.LinearSegmentedColormap.from_list('cmap1', transition_color)\n",
    "kernel = generate_gaussian_kernel(r,s)\n",
    "tmp = _flatmap[...,0]*0.0\n",
    "soma_list = [[],[]]\n",
    "for i in info:\n",
    "    d,p = info[i][:2]\n",
    "    e = info[i][2]\n",
    "    fill_kernel(tmp,np.array(p[::-1])*2,e/neuron_to_brainstem_information[i]['relative_density'],r,kernel)\n",
    "    soma_list[0].append(np.array(p[::-1])*2)\n",
    "    soma_list[1].append(e)\n",
    "soma_list[1] = np.array(soma_list[1])**0.5\n",
    "soma_list[1] /= np.max(soma_list[1])\n",
    "#result = plot_transition_color(transition_color,tmp)\n",
    "result = np.where(_flatmap[...,0]==-1,0,tmp)\n",
    "for k,l in zip(soma_list[0],soma_list[1]):\n",
    "    cv.circle(result,[int(np.round(k[1])), int(np.round(k[0]))],int(np.round(l*10))+1,np.array([0.5,0.5,0.5]),-1)\n",
    "fig = plt.figure(figsize=(7,5))\n",
    "result /= np.max(result)\n",
    "plt.imshow(result,cmap=cmap1,vmin=0,vmax=1)\n",
    "plt.colorbar(extend='both')\n",
    "plt.imshow(flatmap,zorder=1)\n",
    "plt.axis('off')\n",
    "plt.xlim(flatten_map_r[0],flatten_map_r[1])\n",
    "plt.ylim(flatten_map_r[2],flatten_map_r[3])\n",
    "plt.title('RF projection',fontsize=fontsize_title)\n",
    "plt.savefig(medulla_path+'RF_origion.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### detail statics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "source = ['MOs','MOp','AI','GU','VISC','SSs','SSp','ACA']\n",
    "target = ['PARN','IRN','GRN','MARN','PGRNd','PGRNl','MDRNd','MDRNv','LRNm','LRNp','LIN']\n",
    "X = {}\n",
    "for neuron in neuron_to_brainstem_information:\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']\n",
    "    if region not in source:\n",
    "        continue\n",
    "    if region not in X:\n",
    "        X[region] = []\n",
    "    tmp = []\n",
    "    for j in target:\n",
    "        tmp.append(neuron_to_brainstem_information[neuron]['axonal_projection_terminal_combined'][brainstem_region.index(j)])\n",
    "    X[region].append(tmp)\n",
    "width = 0.06\n",
    "scale = 1.4\n",
    "fig = plt.figure(figsize=(12,5))\n",
    "ax = fig.add_subplot(2,1,1)\n",
    "for i,j in enumerate(source):\n",
    "    ax.bar([k+i*width*scale for k in range(len(target))],np.mean(X[j],axis=0),width=width,color=color_pool[i],label=j)\n",
    "    #plt.plot(np.mean(X[j],axis=0),color=color_pool[i],label=j)\n",
    "for i,j in enumerate(target[:-1]):\n",
    "    ax.axvline(i+0.775,color='k',linestyle='--',linewidth=0.75)\n",
    "ax.set_xlim(-0.5,len(target))\n",
    "ax.set_xticks([],[])\n",
    "ax.set_ylabel('Average terminal number',fontsize=fontsize_label*0.6,rotation=-90).set_verticalalignment('top')\n",
    "ax.legend(handles=[mpatches.Patch(color=color_pool[i], label=j) for i,j in enumerate(source)],loc='lower right')\n",
    "ax = fig.add_subplot(2,1,2)\n",
    "tmp = {}\n",
    "for i in X:\n",
    "    module = cortical_module_transform[i]\n",
    "    if module not in tmp:\n",
    "        tmp[module] = []\n",
    "    tmp[module] += X[i]\n",
    "for i,j in enumerate(tmp):\n",
    "    c = np.sum(tmp[j],axis=0)\n",
    "    c = c/np.linalg.norm(c)\n",
    "    ax.plot([i+width*scale*0.5*(len(source)-2+0.5) for i in range(len(target))],c,color=cortical_module_color[j],label=j)\n",
    "    ax.scatter([i+width*scale*0.5*(len(source)-2+0.5) for i in range(len(target))],c,color=cortical_module_color[j],s=10,marker='o',label=j)\n",
    "for i,j in enumerate(target[:-1]):\n",
    "    ax.axvline(i+0.775,color='k',linestyle='--',linewidth=0.75)\n",
    "ax.set_xlim(-0.5,len(target))\n",
    "ax.set_xticks([i+width*scale*0.5*(len(source)-2+0.5) for i in range(len(target))],target,fontsize=fontsize_ticks)\n",
    "ax.set_xlabel('Target',fontsize=fontsize_label)\n",
    "ax.set_ylim(0,1)\n",
    "ax.set_ylabel('Normalized distribution',fontsize=fontsize_label*0.6,rotation=-90).set_verticalalignment('top')\n",
    "ax.legend(handles=[mpatches.Patch(color=cortical_module_color[j],label=j) for i,j in enumerate(tmp)],loc='upper right')\n",
    "plt.suptitle(\"RF terminal statics\",fontsize=fontsize_title)\n",
    "plt.savefig(medulla_path+'RF_statics.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### MO_Insula"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "source = ['Insula','MO']\n",
    "target = ['PARN','IRN','GRN']\n",
    "mask = 0.0\n",
    "for i,j in enumerate(target):\n",
    "    mask = np.where(get_region_mask(j),i+1,mask)\n",
    "X = {}\n",
    "for id,neuron in enumerate(neuron_to_brainstem_information):\n",
    "    region = cortical_module_transform[neuron_to_brainstem_information[neuron]['cortex_region']]\n",
    "    if region not in source:\n",
    "        continue\n",
    "    if region not in X:\n",
    "        X[region] = {'terminal':[]}\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            X[region]['terminal'].append(terminal.copy())\n",
    "            if neuron_terminals[neuron]['mirror_mark']:\n",
    "                X[region]['terminal'][-1][2] = 11400-X[region]['terminal'][-1][2]\n",
    "mask_2D = np.sum(mask,axis=0)\n",
    "a,b = get_value_range(mask_2D)\n",
    "tmp = [np.mean(a),np.mean(b)]\n",
    "tmp2 = np.max([a[1]-a[0],b[1]-b[0]])\n",
    "_r = [tmp[0]-tmp2/2,tmp[0]+tmp2/2,tmp[1]+tmp2/2,tmp[1]-tmp2/2]\n",
    "m = 1-get_contour(mask_2D>0)\n",
    "T = {}\n",
    "for i,j in enumerate(target):\n",
    "    m = np.where(get_contour(np.sum(mask==i+1,axis=0)>0),0,m)\n",
    "    tmp = np.sum(mask==i+1,axis=0)\n",
    "    tmp[...,570:] *= 0\n",
    "    a,b = get_value_range(tmp)\n",
    "    T[j] = [np.mean(a),np.mean(b)]\n",
    "fig,ax = plt.subplots(figsize=(5,5))\n",
    "ax.matshow(m,cmap='gray')\n",
    "_color = {'Insula':color_pool[3],'MO':color_pool[0]}\n",
    "for i in X:\n",
    "    tmp = np.array(X[i]['terminal'])\n",
    "    ax.scatter(tmp[:,2]/10,tmp[:,1]/10,color=_color[i],s=0.4)\n",
    "for i in T:\n",
    "    ax.text(T[i][0]-10,T[i][1],i,fontsize=fontsize_ticks)\n",
    "ax.set_xlim(_r[0],_r[1])\n",
    "ax.set_ylim(_r[2],_r[3])\n",
    "ax.set_title(\"Terminal in reticular formation\",fontsize=fontsize_title)\n",
    "ax.set_axis_off()\n",
    "ax.legend(handles=[mpatches.Patch(color=_color[i], label=i) for i in _color],loc='lower right')\n",
    "plt.savefig(medulla_path+'Terminal_in_RF_Insula_MO.pdf',format='pdf',bbox_inches='tight',dpi=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure(figsize=(12,5))\n",
    "ax = fig.add_subplot(1,2,1)\n",
    "parts = ax.violinplot([np.array(X[i]['terminal'])[:,2] for i in ['Insula','MO']],showmeans=False,showextrema=False,showmedians=False,vert=False)\n",
    "for pc,c in zip(parts['bodies'],[3,0]):\n",
    "    pc.set_facecolor(color_pool[c])\n",
    "    pc.set_edgecolor('black')\n",
    "    pc.set_alpha(1)\n",
    "ax.axvline(5700,color='k',linestyle='--')\n",
    "ax.invert_yaxis()\n",
    "ax.set_xticks([5700-2500,5700,5700+2500],['ipsilateral\\n(3200)','midline\\n(5700)','contralatel\\n(8200)'],fontsize=fontsize_ticks)\n",
    "ax.set_yticks([1,2],['Insula','MO'],fontsize=fontsize_ticks)\n",
    "ax.set_xlabel('Distance from lateral (\\u00B5m)',fontsize=fontsize_label)\n",
    "ax.set_ylabel('Modality',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "ax.set_title('Lateral-medial axis',fontsize=fontsize_label*0.8)\n",
    "ax.legend(handles=[mpatches.Patch(color=_color[i], label=i) for i in _color],loc='lower right')\n",
    "ax = fig.add_subplot(1,2,2)\n",
    "parts = ax.violinplot([np.array(X[i]['terminal'])[:,1] for i in ['Insula','MO']],showmeans=False,showextrema=False,showmedians=False)\n",
    "for pc,c in zip(parts['bodies'],[3,0]):\n",
    "    pc.set_facecolor(color_pool[c])\n",
    "    pc.set_edgecolor('black')\n",
    "    pc.set_alpha(1)\n",
    "ax.invert_yaxis()\n",
    "ax.set_xticks([i+1 for i in range(len(X))],[i for i in X],fontsize=fontsize_ticks)\n",
    "ax.set_xlabel('Modality',fontsize=fontsize_label)\n",
    "ax.set_ylabel('Distance from dorsal (\\u00B5m)',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "ax.set_title('Dorsal-ventral axis',fontsize=fontsize_label*0.8)\n",
    "plt.suptitle('Terminal distribution in dorsal-ventral axis',fontsize=fontsize_title)\n",
    "plt.savefig(medulla_path+'Terminal_in_RF_Insula_MO_bilateral.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in ['Insula','MO']:\n",
    "    tmp = rotate_array(np.array(X[i]['terminal'])[:,-1:0:-1],-160.0/180.0*3.14)\n",
    "    plt.scatter(tmp[:,0],tmp[:,1],color=_color[i],s=0.25)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def rotation_matrix(theta):\n",
    "    return np.array([[np.cos(theta), -np.sin(theta)],\n",
    "                     [np.sin(theta), np.cos(theta)]])\n",
    "def rotate_array(arr, theta):\n",
    "    rot_matrix = rotation_matrix(theta)\n",
    "    rot_arr = np.dot(arr, rot_matrix)\n",
    "    return rot_arr\n",
    "fig = plt.figure(figsize=(5,5))\n",
    "ax = fig.add_subplot(1,1,1)\n",
    "tmp = [np.array(X[i]['terminal'])[:,-1:0:-1] for i in ['Insula','MO']]\n",
    "for i in range(2):\n",
    "    tmp[i][:,1] = np.where(tmp[i][:,1]<5700,11400-tmp[i][:,1],tmp[i][:,1])\n",
    "tmp = [rotate_array(i,-160.0/180.0*3.14)[:,1] for i in tmp]\n",
    "parts = ax.violinplot(tmp,showmeans=False,showextrema=False,showmedians=False)\n",
    "for pc,c in zip(parts['bodies'],[3,0]):\n",
    "    pc.set_facecolor(color_pool[c])\n",
    "    pc.set_edgecolor('black')\n",
    "    pc.set_alpha(1)\n",
    "#ax.invert_yaxis()\n",
    "ax.set_xticks([i+1 for i in range(len(X))],[i for i in X],fontsize=fontsize_ticks)\n",
    "ax.set_xlabel('Modality',fontsize=fontsize_label)\n",
    "ax.set_ylabel('Distance from dorsal (\\u00B5m)',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "ax.set_title('Dorsal-ventral axis',fontsize=fontsize_label*0.8)\n",
    "plt.suptitle('Terminal distribution in dorsal-ventral axis',fontsize=fontsize_title)\n",
    "plt.savefig(medulla_path+'Terminal_in_RF_Insula_MO_bilateral2.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### MO_SS"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "source = ['SSs','SSp','MOp','MOs']\n",
    "target = ['PARN','IRN','GRN']\n",
    "mask = 0.0\n",
    "for i,j in enumerate(target):\n",
    "    mask = np.where(get_region_mask(j),i+1,mask)\n",
    "X = {}\n",
    "for id,neuron in enumerate(neuron_to_brainstem_information):\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']\n",
    "    if region not in source:\n",
    "        continue\n",
    "    if region not in X:\n",
    "        X[region] = {'terminal':[]}\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            X[region]['terminal'].append(terminal.copy())\n",
    "            if neuron_terminals[neuron]['mirror_mark']:\n",
    "                X[region]['terminal'][-1][2] = 11400-X[region]['terminal'][-1][2]\n",
    "mask_2D = np.sum(mask,axis=0)\n",
    "a,b = get_value_range(mask_2D)\n",
    "tmp = [np.mean(a),np.mean(b)]\n",
    "tmp2 = np.max([a[1]-a[0],b[1]-b[0]])\n",
    "_r = [tmp[0]-tmp2/2,tmp[0]+tmp2/2,tmp[1]+tmp2/2,tmp[1]-tmp2/2]\n",
    "m = 1-get_contour(mask_2D>0)\n",
    "T = {}\n",
    "for i,j in enumerate(target):\n",
    "    m = np.where(get_contour(np.sum(mask==i+1,axis=0)>0),0,m)\n",
    "    tmp = np.sum(mask==i+1,axis=0)\n",
    "    tmp[...,570:] *= 0\n",
    "    a,b = get_value_range(tmp)\n",
    "    T[j] = [np.mean(a),np.mean(b)]\n",
    "fig,ax = plt.subplots(figsize=(5,5))\n",
    "ax.matshow(m,cmap='gray')\n",
    "_color = {'MOs':color_pool[0],'MOp':color_pool[3],'SSp':color_pool[1],'SSs':color_pool[2]}\n",
    "for i in X:\n",
    "    tmp = np.array(X[i]['terminal'])\n",
    "    ax.scatter(tmp[:,2]/10,tmp[:,1]/10,color=_color[i],s=0.4)\n",
    "for i in T:\n",
    "    ax.text(T[i][0]-10,T[i][1],i,fontsize=fontsize_ticks)\n",
    "ax.set_xlim(_r[0],_r[1])\n",
    "ax.set_ylim(_r[2],_r[3])\n",
    "ax.set_title(\"Terminal in reticular formation\",fontsize=fontsize_title)\n",
    "ax.set_axis_off()\n",
    "ax.legend(handles=[mpatches.Patch(color=_color[i], label=i) for i in _color],loc='lower right')\n",
    "plt.savefig(medulla_path+'Terminal_in_RF_SS_MO.pdf',format='pdf',bbox_inches='tight',dpi=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure(figsize=(12,5))\n",
    "ax = fig.add_subplot(1,2,1)\n",
    "parts = ax.violinplot([np.array(X[i]['terminal'])[:,2] for i in source],showmeans=False,showextrema=False,showmedians=False,vert=False)\n",
    "for pc,c in zip(parts['bodies'],[2,1,3,0]):\n",
    "    pc.set_facecolor(color_pool[c])\n",
    "    pc.set_edgecolor('black')\n",
    "    pc.set_alpha(1)\n",
    "ax.axvline(5700,color='k',linestyle='--')\n",
    "ax.invert_yaxis()\n",
    "ax.set_xticks([5700-2500,5700,5700+2500],['ipsilateral\\n(3200)','midline\\n(5700)','contralatel\\n(8200)'],fontsize=fontsize_ticks)\n",
    "ax.set_yticks([1,2],['Insula','MO'],fontsize=fontsize_ticks)\n",
    "ax.set_xlabel('Distance from lateral (\\u00B5m)',fontsize=fontsize_label)\n",
    "ax.set_ylabel('Modality',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "ax.set_title('Lateral-medial axis',fontsize=fontsize_label*0.8)\n",
    "ax.legend(handles=[mpatches.Patch(color=_color[i], label=i) for i in _color],loc='lower right')\n",
    "ax = fig.add_subplot(1,2,2)\n",
    "parts = ax.violinplot([np.array(X[i]['terminal'])[:,1] for i in source],showmeans=False,showextrema=False,showmedians=False)\n",
    "for pc,c in zip(parts['bodies'],[2,1,3,0]):\n",
    "    pc.set_facecolor(color_pool[c])\n",
    "    pc.set_edgecolor('black')\n",
    "    pc.set_alpha(1)\n",
    "ax.invert_yaxis()\n",
    "ax.set_xticks([i+1 for i in range(len(X))],[i for i in X],fontsize=fontsize_ticks)\n",
    "ax.set_xlabel('Modality',fontsize=fontsize_label)\n",
    "ax.set_ylabel('Distance from dorsal (\\u00B5m)',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "ax.set_title('Dorsal-ventral axis',fontsize=fontsize_label*0.8)\n",
    "plt.suptitle('Terminal distribution in dorsal-ventral axis',fontsize=fontsize_title)\n",
    "plt.savefig(medulla_path+'Terminal_in_RF_SS_MP_bilateral.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in source:\n",
    "    e = np.array(X[i]['terminal'])[:,-1:0:-1]\n",
    "    e[:,0] = np.where(e[:,0]<5700,11400-e[:,0],e[:,0])\n",
    "    tmp = rotate_array(e,-140.0/180.0*3.14)\n",
    "    plt.scatter(tmp[:,0],tmp[:,1],color=_color[i],s=0.25)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def rotation_matrix(theta):\n",
    "    return np.array([[np.cos(theta), -np.sin(theta)],\n",
    "                     [np.sin(theta), np.cos(theta)]])\n",
    "def rotate_array(arr, theta):\n",
    "    rot_matrix = rotation_matrix(theta)\n",
    "    rot_arr = np.dot(arr, rot_matrix)\n",
    "    return rot_arr\n",
    "fig = plt.figure(figsize=(5,5))\n",
    "ax = fig.add_subplot(1,1,1)\n",
    "tmp = [np.array(X[i]['terminal'])[:,-1:0:-1] for i in source]\n",
    "for i in range(len(source)):\n",
    "    tmp[i][:,0] = np.where(tmp[i][:,0]<5700,11400-tmp[i][:,0],tmp[i][:,0])\n",
    "tmp = [rotate_array(i,-140.0/180.0*3.14)[:,1] for i in tmp]\n",
    "parts = ax.violinplot(tmp,showmeans=False,showextrema=False,showmedians=False)\n",
    "for pc,c in zip(parts['bodies'],[2,1,3,0]):\n",
    "    pc.set_facecolor(color_pool[c])\n",
    "    pc.set_edgecolor('black')\n",
    "    pc.set_alpha(1)\n",
    "ax.scatter(range(1,len(tmp)+1),[np.median(i) for i in tmp],color='k',s=10)\n",
    "ax.plot(range(1,len(tmp)+1),[np.median(i) for i in tmp],color='k',linestyle='--')\n",
    "#ax.invert_yaxis()\n",
    "ax.set_xticks([i+1 for i in range(len(source))],[i for i in source],fontsize=fontsize_ticks)\n",
    "ax.set_xlabel('Modality',fontsize=fontsize_label)\n",
    "ax.set_ylabel('Distance from dorsal\\n(rotated space \\u00B5m)',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.suptitle('Terminal distribution in dorsal-ventral axis',fontsize=fontsize_title)\n",
    "plt.savefig(medulla_path+'Terminal_in_RF_Insula_MO_bilateral2.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### AI_GU"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "source = ['GU','VISC','AI']\n",
    "target = ['PARN','IRN','GRN']\n",
    "mask = 0.0\n",
    "for i,j in enumerate(target):\n",
    "    mask = np.where(get_region_mask(j),i+1,mask)\n",
    "X = {}\n",
    "for id,neuron in enumerate(neuron_to_brainstem_information):\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']\n",
    "    if region not in source:\n",
    "        continue\n",
    "    if region not in X:\n",
    "        X[region] = {'terminal':[]}\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            X[region]['terminal'].append(terminal.copy())\n",
    "            if neuron_terminals[neuron]['mirror_mark']:\n",
    "                X[region]['terminal'][-1][2] = 11400-X[region]['terminal'][-1][2]\n",
    "mask_2D = np.sum(mask,axis=0)\n",
    "a,b = get_value_range(mask_2D)\n",
    "tmp = [np.mean(a),np.mean(b)]\n",
    "tmp2 = np.max([a[1]-a[0],b[1]-b[0]])\n",
    "_r = [tmp[0]-tmp2/2,tmp[0]+tmp2/2,tmp[1]+tmp2/2,tmp[1]-tmp2/2]\n",
    "m = 1-get_contour(mask_2D>0)\n",
    "T = {}\n",
    "for i,j in enumerate(target):\n",
    "    m = np.where(get_contour(np.sum(mask==i+1,axis=0)>0),0,m)\n",
    "    tmp = np.sum(mask==i+1,axis=0)\n",
    "    tmp[...,570:] *= 0\n",
    "    a,b = get_value_range(tmp)\n",
    "    T[j] = [np.mean(a),np.mean(b)]\n",
    "fig,ax = plt.subplots(figsize=(5,5))\n",
    "ax.matshow(m,cmap='gray')\n",
    "_color = {'AI':color_pool[0],'GU':color_pool[1],'VISC':color_pool[3]}\n",
    "for i in X:\n",
    "    tmp = np.array(X[i]['terminal'])\n",
    "    ax.scatter(tmp[:,2]/10,tmp[:,1]/10,color=_color[i],s=0.4)\n",
    "for i in T:\n",
    "    ax.text(T[i][0]-10,T[i][1],i,fontsize=fontsize_ticks)\n",
    "ax.set_xlim(_r[0],_r[1])\n",
    "ax.set_ylim(_r[2],_r[3])\n",
    "ax.set_title(\"Terminal in reticular formation\",fontsize=fontsize_title)\n",
    "ax.set_axis_off()\n",
    "ax.legend(handles=[mpatches.Patch(color=_color[i], label=i) for i in _color],loc='lower right')\n",
    "plt.savefig(medulla_path+'Terminal_in_RF_AI_GU_VISC.pdf',format='pdf',bbox_inches='tight',dpi=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure(figsize=(12,5))\n",
    "ax = fig.add_subplot(1,2,1)\n",
    "parts = ax.violinplot([np.array(X[i]['terminal'])[:,2] for i in source],showmeans=False,showextrema=False,showmedians=False,vert=False)\n",
    "for pc,c in zip(parts['bodies'],[3,1,0]):\n",
    "    pc.set_facecolor(color_pool[c])\n",
    "    pc.set_edgecolor('black')\n",
    "    pc.set_alpha(1)\n",
    "ax.axvline(5700,color='k',linestyle='--')\n",
    "ax.invert_yaxis()\n",
    "ax.set_xticks([5700-2500,5700,5700+2500],['ipsilateral\\n(3200)','midline\\n(5700)','contralatel\\n(8200)'],fontsize=fontsize_ticks)\n",
    "ax.set_yticks([1,2,3],source,fontsize=fontsize_ticks)\n",
    "ax.set_xlabel('Distance from lateral (\\u00B5m)',fontsize=fontsize_label)\n",
    "ax.set_ylabel('Modality',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "ax.set_title('Lateral-medial axis',fontsize=fontsize_label*0.8)\n",
    "ax.legend(handles=[mpatches.Patch(color=_color[i], label=i) for i in _color],loc='lower right')\n",
    "ax = fig.add_subplot(1,2,2)\n",
    "parts = ax.violinplot([np.array(X[i]['terminal'])[:,1] for i in source],showmeans=False,showextrema=False,showmedians=False)\n",
    "for pc,c in zip(parts['bodies'],[0,1,3]):\n",
    "    pc.set_facecolor(color_pool[c])\n",
    "    pc.set_edgecolor('black')\n",
    "    pc.set_alpha(1)\n",
    "ax.invert_yaxis()\n",
    "ax.set_xticks([i+1 for i in range(len(X))],[i for i in X],fontsize=fontsize_ticks)\n",
    "ax.set_xlabel('Modality',fontsize=fontsize_label)\n",
    "ax.set_ylabel('Distance from dorsal (\\u00B5m)',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "ax.set_title('Dorsal-ventral axis',fontsize=fontsize_label*0.8)\n",
    "plt.suptitle('Terminal distribution in dorsal-ventral axis',fontsize=fontsize_title)\n",
    "plt.savefig(medulla_path+'Terminal_in_AI_GU_VISC_bilateral.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in source:\n",
    "    e = np.array(X[i]['terminal'])[:,-1:0:-1]\n",
    "    e[:,0] = np.where(e[:,0]<5700,11400-e[:,0],e[:,0])\n",
    "    tmp = rotate_array(e,-140.0/180.0*3.14)\n",
    "    plt.scatter(tmp[:,0],tmp[:,1],color=_color[i],s=0.25)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def rotation_matrix(theta):\n",
    "    return np.array([[np.cos(theta), -np.sin(theta)],\n",
    "                     [np.sin(theta), np.cos(theta)]])\n",
    "def rotate_array(arr, theta):\n",
    "    rot_matrix = rotation_matrix(theta)\n",
    "    rot_arr = np.dot(arr, rot_matrix)\n",
    "    return rot_arr\n",
    "fig = plt.figure(figsize=(5,5))\n",
    "ax = fig.add_subplot(1,1,1)\n",
    "tmp = [np.array(X[i]['terminal'])[:,-1:0:-1] for i in source]\n",
    "for i in range(len(source)):\n",
    "    tmp[i][:,0] = np.where(tmp[i][:,0]<5700,11400-tmp[i][:,0],tmp[i][:,0])\n",
    "tmp = [rotate_array(i,-140.0/180.0*3.14)[:,1] for i in tmp]\n",
    "parts = ax.violinplot(tmp,showmeans=False,showextrema=False,showmedians=False)\n",
    "for pc,c in zip(parts['bodies'],[3,1,0]):\n",
    "    pc.set_facecolor(color_pool[c])\n",
    "    pc.set_edgecolor('black')\n",
    "    pc.set_alpha(1)\n",
    "ax.scatter(range(1,len(tmp)+1),[np.median(i) for i in tmp],color='k',s=10)\n",
    "ax.plot(range(1,len(tmp)+1),[np.median(i) for i in tmp],color='k',linestyle='--')\n",
    "#ax.invert_yaxis()\n",
    "ax.set_xticks([i+1 for i in range(len(source))],[i for i in source],fontsize=fontsize_ticks)\n",
    "ax.set_xlabel('Modality',fontsize=fontsize_label)\n",
    "ax.set_ylabel('Distance from dorsal\\n(rotated space \\u00B5m)',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.suptitle('Terminal distribution in dorsal-ventral axis',fontsize=fontsize_title)\n",
    "plt.savefig(medulla_path+'Terminal_in_RF_AI_GU_VISC_bilateral2.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### MOs in reticular formation detail"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if not os.path.exists(medulla_path+'MO_RF_difference.npy'):\n",
    "    target = ['IRN','GRN','MARN','PGRN','MDRN','LRN','LIN']\n",
    "    mask = 0.0\n",
    "    for i in target:\n",
    "        mask += get_region_mask(i)\n",
    "    result = [np.zeros((1500,800,1140)),np.zeros((1500,800,1140))]\n",
    "    for neuron in tool_bar(motor_neuron_list):\n",
    "        if neuron_to_brainstem_information[neuron]['cortex_region']=='MOp':\n",
    "            c = 0\n",
    "        else:\n",
    "            c = 1\n",
    "        control_console_output(0)\n",
    "        neuron_tree = iondata.getNeuronTreeByID(neuron[:-7],neuron[-7:])\n",
    "        control_console_output(1)\n",
    "        mirror = neuron_terminals[neuron]['mirror_mark']\n",
    "        for terminal in neuron_tree.terminals:\n",
    "            if if_inside_mask(mask,np.array(terminal.xyz)/10):\n",
    "                draw_line_in_mask(result[c],terminal,mirror)\n",
    "    result = np.array(result)\n",
    "    np.save(medulla_path+'MO_RF_difference.npy',result)\n",
    "else:\n",
    "    result = np.load(medulla_path+'MO_RF_difference.npy')\n",
    "tmp = get_region_mask('root')\n",
    "brain_mask = np.zeros((1500,800,1140))\n",
    "brain_mask[:tmp.shape[0]] = tmp*1\n",
    "fig, ax = plt.subplots(2,3,figsize=(9,6))\n",
    "for i in range(2):\n",
    "    for j in range(3):\n",
    "        tmp = np.log(np.sum(result[i],axis=j)+1)\n",
    "        tmp = tmp/np.max(tmp)\n",
    "        m = np.sum(brain_mask,axis=j)>0\n",
    "        ax[i,j].imshow(np.expand_dims(tmp,-1)*color_pool[0]+np.expand_dims(m*(1-tmp),-1)*color_pool[2])\n",
    "        ax[i,j].set_axis_off()\n",
    "plt.savefig(medulla_path+'SS_MO_difference_in_RF.pdf',format='pdf')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mask = get_region_mask('root')\n",
    "my_mask = get_region_mask('MY')\n",
    "plt.figure(figsize=(13,8))\n",
    "a = np.sum(mask,axis=-1).astype(float)>0\n",
    "b = np.sum(my_mask,axis=-1).astype(float)>0\n",
    "c = np.log(np.sum(result[1][:1320],axis=-1)+1)\n",
    "c/= np.max(c)\n",
    "b = get_contour(b)\n",
    "plt.imshow(np.array([c*(1-b)+b,a*0.25*(1-b)+b,b]).transpose(2,1,0))\n",
    "plt.savefig(medulla_path+'MOs_in_RF_lateral.pdf',format='pdf')\n",
    "plt.plot([100,200],[700,700],color=[1.0,1.0,1.0])\n",
    "plt.axis('off')\n",
    "plt.axis(\"equal\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize=(13,11))\n",
    "a = np.sum(mask,axis=1).astype(float)>0\n",
    "b = np.sum(my_mask,axis=1).astype(float)>0\n",
    "c = np.log(np.sum(result[1][:1320],axis=1)+1)\n",
    "c/= np.max(c)\n",
    "b = get_contour(b)\n",
    "plt.imshow(np.array([c*(1-b)+b,a*0.25*(1-b)+b,b]).transpose(1,2,0))\n",
    "plt.plot([100,200],[700,700],color=[1.0,1.0,1.0])\n",
    "plt.axis('off')\n",
    "plt.axis(\"equal\")\n",
    "plt.savefig(medulla_path+'MOs_in_RF_anterior.pdf',format='pdf')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### ipsi contra difference"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = []\n",
    "x_label = []\n",
    "for r in ['SSs','SSp','MOp','MOs']:\n",
    "    neuron_list = []\n",
    "    for i in neuron_to_brainstem_information:\n",
    "        if neuron_to_brainstem_information[i]['cortex_region'] in [r]:\n",
    "            neuron_list.append(i)\n",
    "    target = ['PARN','IRN','GRN','MARN','PGRNl','PGRNd','MDRNd','MDRNv','LRNp','LRNm','LIN','V','VII','DMX','AMB']\n",
    "    neuron_info = get_neuron_axon_info(neuron_list,terminal_or_length='terminal',if_combined=False,if_log=False,filter=target)\n",
    "    a = np.sum(neuron_info[1],axis=0).reshape(2,len(target)).astype(float)\n",
    "    b = np.max(a,axis=-1)\n",
    "    b = [r+' ipsi(max='+str(int(b[0]))+')',r+' contr(max='+str(int(b[1]))+')']\n",
    "    a[0] /= np.max(a[0])\n",
    "    a[1] /= np.max(a[1])\n",
    "    x_label += b\n",
    "    result.append(a)\n",
    "plt.matshow(np.concatenate(result,axis=0),cmap='BuGn')\n",
    "_ = plt.xticks(range(len(target)),target,fontsize=fontsize_ticks,rotation=-45)\n",
    "_ = plt.yticks(range(len(x_label)),x_label,fontsize=fontsize_ticks)\n",
    "plt.title(r+' projection detail (relative terminal ditribution)',fontsize=fontsize_title)\n",
    "plt.colorbar()\n",
    "plt.savefig(medulla_path+'RF_detail.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = ['IRN','GRN','MARN','PGRNl','PGRNd','MDRNd','MDRNv','LRNp','LRNm','LIN','V','VII','DMX','AMB']\n",
    "MO_neuron_info = get_neuron_axon_info(motor_neuron_list,terminal_or_length='terminal',if_combined=True,if_log=False,filter=target)\n",
    "X = [np.sum(MO_neuron_info[1][:,:-4]),np.sum(MO_neuron_info[1][:,-4:])]\n",
    "def func(pct, allvals):\n",
    "    absolute = int(np.round(pct/100.*np.sum(allvals)))\n",
    "    return f\"{pct:.1f}%\\n({absolute:d})\"\n",
    "plt.pie(X,colors=[color_pool[0],color_pool[3]],labels=['reticular formation','motor nuclei'], autopct=lambda pct: func(pct,X),textprops=dict(color=\"w\"))\n",
    "plt.legend()\n",
    "plt.savefig(medulla_path+'MO_RF_M_distribution.pdf',format='pdf')\n",
    "plt.show()\n",
    "MOp_neuron_list = []\n",
    "MOs_neuron_list = []\n",
    "for neuron in motor_neuron_list:\n",
    "    if neuron_to_brainstem_information[neuron]['cortex_region']=='MOp':\n",
    "        MOp_neuron_list.append(neuron)\n",
    "    else:\n",
    "        MOs_neuron_list.append(neuron)\n",
    "MOp_neuron_info = get_neuron_axon_info(MOp_neuron_list,terminal_or_length='terminal',if_combined=True,if_log=False,filter=target)\n",
    "MOs_neuron_info = get_neuron_axon_info(MOs_neuron_list,terminal_or_length='terminal',if_combined=True,if_log=False,filter=target)\n",
    "width = 0.2\n",
    "plt.bar([0,1],[np.sum(np.array(MOp_neuron_info[1])[:,:-4])/len(MOp_neuron_info[1]),np.sum(np.array(MOp_neuron_info[1])[:,-4:])/len(MOp_neuron_info[1])],label='MOp',width=width*0.75,color=color_pool[3])\n",
    "plt.bar(np.array([0,1])+width,[np.sum(np.array(MOs_neuron_info[1])[:,:-4])/len(MOs_neuron_info[1]),np.sum(np.array(MOs_neuron_info[1])[:,-4:])/len(MOs_neuron_info[1])],label='MOs',width=width*0.75,color=color_pool[0])\n",
    "plot_post_process(title='Reticular formation and motor nuclei',xlabel='Modalities',ylabel='Average terminal number',save_path=medulla_path+'MOp_MOs_RF_distribution')\n",
    "plt.xticks(np.array([0,1])+width/2,['reticular formation','motor nuceli'],fontsize=fontsize_ticks)\n",
    "plt.xticks(rotation = 0)\n",
    "plt.legend()\n",
    "plt.savefig(medulla_path+'MOp_MOs_RF_distribution.pdf',format='pdf')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = ['IRN','GRN','MARN','PGRNl','PGRNd','MDRNd','MDRNv','LRNp','LRNm','LIN','V','VII','DMX','AMB']\n",
    "MO_neuron_info = get_neuron_axon_info(motor_neuron_list,terminal_or_length='terminal',if_combined=True,if_log=False,filter=target)\n",
    "X = [np.sum(MO_neuron_info[1][:,:-4]),np.sum(MO_neuron_info[1][:,-4:])]\n",
    "def func(pct, allvals):\n",
    "    absolute = int(np.round(pct/100.*np.sum(allvals)))\n",
    "    return f\"{pct:.1f}%\\n({absolute:d})\"\n",
    "plt.pie(X,colors=[color_pool[0],color_pool[3]],labels=['reticular formation','motor nuclei'], autopct=lambda pct: func(pct,X),textprops=dict(color=\"w\"))\n",
    "plt.legend()\n",
    "plt.savefig(medulla_path+'MO_RF_M_distribution.pdf',format='pdf')\n",
    "plt.show()\n",
    "MOp_neuron_list = []\n",
    "MOs_neuron_list = []\n",
    "for neuron in motor_neuron_list:\n",
    "    if neuron_to_brainstem_information[neuron]['cortex_region']=='MOp':\n",
    "        MOp_neuron_list.append(neuron)\n",
    "    else:\n",
    "        MOs_neuron_list.append(neuron)\n",
    "MOp_neuron_info = get_neuron_axon_info(MOp_neuron_list,terminal_or_length='terminal',if_combined=True,if_log=False,filter=target)\n",
    "MOs_neuron_info = get_neuron_axon_info(MOs_neuron_list,terminal_or_length='terminal',if_combined=True,if_log=False,filter=target)\n",
    "width = 0.2\n",
    "plt.bar([0,1],[np.sum(np.array(MOp_neuron_info[1])[:,:-4])/len(MOp_neuron_info[1]),np.sum(np.array(MOp_neuron_info[1])[:,-4:])/len(MOp_neuron_info[1])],label='MOp',width=width*0.75,color=color_pool[3])\n",
    "plt.bar(np.array([0,1])+width,[np.sum(np.array(MOs_neuron_info[1])[:,:-4])/len(MOs_neuron_info[1]),np.sum(np.array(MOs_neuron_info[1])[:,-4:])/len(MOs_neuron_info[1])],label='MOs',width=width*0.75,color=color_pool[0])\n",
    "plot_post_process(title='Reticular formation and motor nuclei',xlabel='Modalities',ylabel='Average terminal number',save_path=medulla_path+'MOp_MOs_RF_distribution')\n",
    "plt.xticks(np.array([0,1])+width/2,['reticular formation','motor nuceli'],fontsize=fontsize_ticks)\n",
    "plt.xticks(rotation = 0)\n",
    "plt.legend()\n",
    "plt.savefig(medulla_path+'MOp_MOs_RF_distribution.pdf',format='pdf')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = ['IRN','GRN','MARN','PGRN','MDRN','LRN','LIN']\n",
    "mask = 0.0\n",
    "for i in target:\n",
    "    mask += get_region_mask(i)\n",
    "result = [[],[],[]]\n",
    "for neuron in tool_bar(motor_neuron_list):\n",
    "    if neuron_to_brainstem_information[neuron]['cortex_region'] not in ['MOp','MOs']:\n",
    "        continue\n",
    "    l = neuron_to_brainstem_information[neuron]['soma_flatten_position']\n",
    "    k = l[0]+0.2*l[1]\n",
    "    if k>970:\n",
    "        c = 0\n",
    "    elif k>870:\n",
    "        c = 1\n",
    "    else:\n",
    "        c = 2\n",
    "    mirror = neuron_terminals[neuron]['mirror_mark']\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            result[c].append(terminal.copy())\n",
    "            if mirror:\n",
    "                result[c][-1][2] = 11400-result[c][-1][2]\n",
    "g_c = {0:3,1:0,2:1}\n",
    "g_l = {0:'bfd',1:'n',2:'m'}\n",
    "from mpl_toolkits.axes_grid1 import make_axes_locatable\n",
    "fig, ax = plt.subplots(figsize=(15,5))\n",
    "ax_histy = make_axes_locatable(ax).append_axes(\"right\", 0.5, pad=0.1, sharey=ax)\n",
    "ax.imshow(np.sum(mask,axis=-1).T>0,cmap='gray')\n",
    "A = np.array([i for i in range(mask.shape[1])])\n",
    "for g in range(3):\n",
    "    tmp = np.array(result[g])\n",
    "    ax.scatter(tmp[:,0]/10,tmp[:,1]/10,s=1,label=g_l[g],c=color_pool[g_c[g]])\n",
    "    tmp2 = 0.0\n",
    "    for k in tmp[:,1]:\n",
    "        tmp2 += func_gaussian(A,k/10,7)\n",
    "    ax_histy.plot(tmp2/np.sum(tmp2),A,label=g_l[g],c=color_pool[g_c[g]])\n",
    "z = np.sum(mask,axis=-1)\n",
    "x = (np.sum(z,axis=-1)>0).tolist()\n",
    "ax.set_xlim(x.index(1),len(x)-1-x[::-1].index(1))\n",
    "x = (np.sum(z,axis=0)>0).tolist()\n",
    "ax.set_ylim(len(x)-1-x[::-1].index(1),x.index(1))\n",
    "plt.legend()\n",
    "ax.set_axis_off()\n",
    "ax.set_aspect('equal', adjustable='box')\n",
    "_ = ax_histy.set_yticks([],[])\n",
    "plt.savefig(medulla_path+'MO_terminal_in_RF_modality.pdf',format='pdf')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### how reticular formation project"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuron_list = {}\n",
    "for i in ['GRN','MDRNv','IRN','PARN','MDRNd','SPVO','SPVI','SPVC']:\n",
    "    neuron_list[i] = []\n",
    "    for j in iondata.getNeuronListBySomaRegion(i):\n",
    "        if j['region']==i and len(j['sampleid'])==6 and j['sampleid'][0] not in ['A','0']:\n",
    "            neuron_property = iondata.getNeuronPropertyByID(j['sampleid'],j['name'])\n",
    "            neuron_list[i].append([j['sampleid']+j['name'],neuron_property['somapoint']])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in neuron_list:\n",
    "    print(i,len(neuron_list[i]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mask = 0.0\n",
    "for i,j in enumerate(['SPVO','SPVI','SPVC']):\n",
    "    mask = np.where(get_region_mask(j),i+1,mask)\n",
    "plt.matshow(np.sum(mask,axis=-1).T)\n",
    "plt.plot([1170,1170],[0,800])\n",
    "plt.plot([1250,1250],[0,800])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "for i in [['SPVO','SPVI','SPVC'],['PARN','MDRNd'],['IRN'],['GRN','MDRNv']]:\n",
    "    neuronvis.clear()\n",
    "    for j in i:\n",
    "        for k in neuron_list[j]:\n",
    "            if k[1][0]<(11700+12500)/2:\n",
    "                color_index = 0\n",
    "            else:\n",
    "                color_index = 3\n",
    "            neuronvis.addNeuronByID(k[0][:-7],k[0][-7:],color_pool[color_index],mirrorToRight=True,somaHide=False,dendriteHide=True)\n",
    "    for j in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "        set_view(neuronvis,j)\n",
    "        neuronvis.render.savepng(medulla_path+'reticular_neuron_'+'_'.join(i)+'_'+j+'.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "for i in [['SPVO','SPVI','SPVC'],['PARN','MDRNd'],['IRN'],['GRN','MDRNv']]:\n",
    "    neuronvis.clear()\n",
    "    for j in i:\n",
    "        for k in neuron_list[j]:\n",
    "            if k[1][0]<11700:\n",
    "                color_index = 0\n",
    "            elif k[1][0]<12500:\n",
    "                color_index = 1\n",
    "            else:\n",
    "                color_index = 3\n",
    "            neuronvis.addNeuronByID(k[0][:-7],k[0][-7:],color_pool[color_index],mirrorToRight=True,somaHide=False,dendriteHide=True)\n",
    "    for j in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "        set_view(neuronvis,j)\n",
    "        neuronvis.render.savepng(medulla_path+'reticular_neuron_'+'_'.join(i)+'_'+j+'.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### reticular formation arrangement"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuron_color_mapping = []\n",
    "name = []\n",
    "for i in tool_bar(cortex_neuron_name,desc='iteration on neurons (generating mapping colors) ...',leave=False,colour='#55AF7B'):\n",
    "    if neuron_to_brainstem_information[i]['cortex_region'] not in ['MOs']:\n",
    "        continue\n",
    "    neuron_color_mapping.append(neuron_to_brainstem_information[i]['soma_flatten_position'])\n",
    "    name.append(i)\n",
    "tmp = position_color_mapping(np.array(neuron_color_mapping),2)\n",
    "neuron_color_mapping = {}\n",
    "for i,j in zip(name,tmp):\n",
    "    neuron_color_mapping[i] = j\n",
    "target = ['PARN','IRN','GRN']\n",
    "mask = 0.0\n",
    "for i,j in enumerate(target):\n",
    "    mask = np.where(get_region_mask(j),i+1,mask)\n",
    "X = {}\n",
    "for id,neuron in enumerate(name):\n",
    "    X[neuron] = {'color':neuron_color_mapping[neuron],'terminal':[]}\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            X[neuron]['terminal'].append(terminal.copy())\n",
    "            if neuron_terminals[neuron]['mirror_mark']:\n",
    "                X[neuron]['terminal'][-1][2] = 11400-X[neuron]['terminal'][-1][2]\n",
    "mask_2D = np.sum(mask,axis=0)\n",
    "a,b = get_value_range(mask_2D)\n",
    "tmp = [np.mean(a),np.mean(b)]\n",
    "tmp2 = np.max([a[1]-a[0],b[1]-b[0]])\n",
    "_r = [tmp[0]-tmp2/2,tmp[0]+tmp2/2,tmp[1]+tmp2/2,tmp[1]-tmp2/2]\n",
    "m = 1-get_contour(mask_2D>0)\n",
    "T = {}\n",
    "for i,j in enumerate(target):\n",
    "    m = np.where(get_contour(np.sum(mask==i+1,axis=0)>0),0,m)\n",
    "    tmp = np.sum(mask==i+1,axis=0)\n",
    "    tmp[...,570:] *= 0\n",
    "    a,b = get_value_range(tmp)\n",
    "    T[j] = [np.mean(a),np.mean(b)]\n",
    "fig,ax = plt.subplots(figsize=(5,5))\n",
    "ax.matshow(m,cmap='gray')\n",
    "for i in X:\n",
    "    if len(X[i]['terminal'])==0:\n",
    "        continue\n",
    "    tmp = np.array(X[i]['terminal'])\n",
    "    ax.scatter(tmp[:,2]/10,tmp[:,1]/10,color=X[i]['color'],s=0.25)\n",
    "for i in T:\n",
    "    ax.text(T[i][0]-10,T[i][1],i,fontsize=fontsize_ticks)\n",
    "ax.set_xlim(_r[0],_r[1])\n",
    "ax.set_ylim(_r[2],_r[3])\n",
    "ax.set_title(\"MOs terminal in reticular\\n(cronal view)\",fontsize=fontsize_title)\n",
    "ax.set_axis_off()\n",
    "#ax.legend(handles=[mpatches.Patch(color=_color[i], label=i) for i in _color],loc='lower right')\n",
    "plt.savefig(medulla_path+'Terminal_in_reticular_MOs_cronal.pdf',format='pdf',bbox_inches='tight',dpi=100)\n",
    "mask_2D = np.sum(mask,axis=-1).T\n",
    "a,b = get_value_range(mask_2D)\n",
    "tmp = [np.mean(a),np.mean(b)]\n",
    "tmp2 = np.max([a[1]-a[0],b[1]-b[0]])\n",
    "_r = [tmp[0]-tmp2/2,tmp[0]+tmp2/2,tmp[1]+tmp2/2,tmp[1]-tmp2/2]\n",
    "m = 1-get_contour(mask_2D>0)\n",
    "T = {}\n",
    "for i,j in enumerate(target):\n",
    "    m = np.where(get_contour(np.sum(mask==i+1,axis=-1).T>0),0,m)\n",
    "    tmp = np.sum(mask==i+1,axis=-1).T\n",
    "    #tmp[...,:570] *= 0\n",
    "    a,b = get_value_range(tmp)\n",
    "    T[j] = [np.mean(a),np.mean(b)]\n",
    "fig,ax = plt.subplots(figsize=(5,5))\n",
    "ax.matshow(m,cmap='gray')\n",
    "for i in X:\n",
    "    if len(X[i]['terminal'])==0:\n",
    "        continue\n",
    "    tmp = np.array(X[i]['terminal'])\n",
    "    ax.scatter(tmp[:,0]/10,tmp[:,1]/10,color=X[i]['color'],s=0.25)\n",
    "for i in T:\n",
    "    ax.text(T[i][0]-10,T[i][1],i,fontsize=fontsize_ticks)\n",
    "ax.set_xlim(_r[0],_r[1])\n",
    "ax.set_ylim(_r[2],_r[3])\n",
    "ax.set_title(\"MOs terminal in reticular\\n(sagittal view)\",fontsize=fontsize_title)\n",
    "ax.set_axis_off()\n",
    "#ax.legend(handles=[mpatches.Patch(color=_color[i], label=i) for i in _color],loc='lower right')\n",
    "plt.savefig(medulla_path+'Terminal_in_reticular_MOs_sagittal.pdf',format='pdf',bbox_inches='tight',dpi=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuron_color_mapping = []\n",
    "name = []\n",
    "for i in tool_bar(cortex_neuron_name,desc='iteration on neurons (generating mapping colors) ...',leave=False,colour='#55AF7B'):\n",
    "    if neuron_to_brainstem_information[i]['cortex_region'] not in ['MOs']:\n",
    "        continue\n",
    "    neuron_color_mapping.append(neuron_to_brainstem_information[i]['soma_flatten_position'])\n",
    "    name.append(i)\n",
    "tmp = position_color_mapping(np.array(neuron_color_mapping),2)\n",
    "neuron_color_mapping = {}\n",
    "for i,j in zip(name,tmp):\n",
    "    neuron_color_mapping[i] = j\n",
    "target = ['MY-mot']\n",
    "mask = 0.0\n",
    "for i,j in enumerate(target):\n",
    "    mask = np.where(get_region_mask(j),i+1,mask)\n",
    "X = {}\n",
    "for id,neuron in enumerate(name):\n",
    "    X[neuron] = {'color':neuron_color_mapping[neuron],'terminal':[]}\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            X[neuron]['terminal'].append(terminal.copy())\n",
    "            if neuron_terminals[neuron]['mirror_mark']:\n",
    "                X[neuron]['terminal'][-1][2] = 11400-X[neuron]['terminal'][-1][2]\n",
    "mask_2D = np.sum(mask,axis=1)\n",
    "a,b = get_value_range(mask_2D)\n",
    "tmp = [np.mean(a),np.mean(b)]\n",
    "tmp2 = np.max([a[1]-a[0],b[1]-b[0]])\n",
    "_r = [tmp[0]-tmp2/2,tmp[0]+tmp2/2,tmp[1]+tmp2/2,tmp[1]-tmp2/2]\n",
    "m = 1-get_contour(mask_2D>0)\n",
    "T = {}\n",
    "for i,j in enumerate(target):\n",
    "    m = np.where(get_contour(np.sum(mask==i+1,axis=1)>0),0,m)\n",
    "    tmp = np.sum(mask==i+1,axis=1)\n",
    "    tmp[...,570:] *= 0\n",
    "    a,b = get_value_range(tmp)\n",
    "    T[j] = [np.mean(a),np.mean(b)]\n",
    "fig,ax = plt.subplots(figsize=(5,5))\n",
    "ax.matshow(m,cmap='gray')\n",
    "for i in X:\n",
    "    if len(X[i]['terminal'])==0:\n",
    "        continue\n",
    "    tmp = np.array(X[i]['terminal'])\n",
    "    ax.scatter(tmp[:,2]/10,tmp[:,0]/10,color=X[i]['color'],s=0.25)\n",
    "for i in T:\n",
    "    ax.text(T[i][0]-10,T[i][1],i,fontsize=fontsize_ticks)\n",
    "ax.set_xlim(_r[0],_r[1])\n",
    "ax.set_ylim(_r[2],_r[3])\n",
    "ax.set_title(\"MOs terminal in reticular\\n(dorsal view)\",fontsize=fontsize_title)\n",
    "ax.set_axis_off()\n",
    "#ax.legend(handles=[mpatches.Patch(color=_color[i], label=i) for i in _color],loc='lower right')\n",
    "plt.savefig(medulla_path+'Terminal_in_reticular_MOs_dorsal.pdf',format='pdf',bbox_inches='tight',dpi=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "er = [0,0]\n",
    "for i in X:\n",
    "    if len(X[i]['terminal'])==0:\n",
    "        continue\n",
    "    tmp = np.array(X[i]['terminal'])\n",
    "    er[0] += np.sum(tmp[:,2]>5700)\n",
    "    er[1] += np.sum(tmp[:,2]<5700)\n",
    "er"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp = _flatmap*1\n",
    "for i in tool_bar(name):\n",
    "        l = neuron_to_brainstem_information[i]['soma_flatten_position']\n",
    "        cv.circle(tmp,[int(l[0])*2, int(l[1])*2],10,np.array(neuron_color_mapping[i])*255,-1)\n",
    "fig,ax = plt.subplots(figsize=(5,5))\n",
    "ax.imshow(tmp,zorder=0)\n",
    "ax.imshow(flatmap_mask,zorder=1)\n",
    "ax.imshow(flatmap,zorder=2)\n",
    "ax.set_xlim(flatten_map_r[0],flatten_map_r[1])\n",
    "ax.set_ylim(flatten_map_r[2],flatten_map_r[3])\n",
    "ax.set_title('Soma position coding',color='k',fontsize=fontsize_title)\n",
    "ax.set_axis_off()\n",
    "plt.savefig(medulla_path+'MOs_soma_coding.pdf',format='pdf',bbox_inches='tight',dpi=100)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "group_color = np.array([[120,214,230],[187,48,84]])/255\n",
    "tmp = [[],[]]\n",
    "for i in X:\n",
    "    g = np.argmin(np.sum((group_color-X[i]['color'])**2,axis=-1))\n",
    "    tmp[g] += X[i]['terminal']\n",
    "for i in range(2):\n",
    "    tmp[i] = np.array(tmp[i])[:,2]\n",
    "    tmp[i] = np.where(tmp[i]<5700,11400-tmp[i],tmp[i])\n",
    "fig = plt.figure(figsize=(5,5))\n",
    "ax = fig.add_subplot(1,1,1)\n",
    "parts = ax.violinplot(tmp,showmeans=False,showextrema=False,showmedians=False,vert=False)\n",
    "for pc,c in zip(parts['bodies'],group_color):\n",
    "    pc.set_facecolor(c)\n",
    "    pc.set_edgecolor('black')\n",
    "    pc.set_alpha(1)\n",
    "ax.scatter([np.median(i) for i in tmp],range(1,len(tmp)+1),color='k',s=10)\n",
    "ax.plot([np.median(i) for i in tmp],range(1,len(tmp)+1),color='k',linestyle='--')\n",
    "ax.axvline(5700,color='k',linestyle='--')\n",
    "ax.set_yticks([1,2],['blue group','red group'],fontsize=fontsize_ticks)\n",
    "ax.set_xlabel('Distance from lateral\\n(rotated space \\u00B5m)',fontsize=fontsize_label)\n",
    "ax.set_ylabel('Modality',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.suptitle('Terminal distribution in lateral-medial axis',fontsize=fontsize_title)\n",
    "plt.savefig(medulla_path+'Terminal_in_reticular_MOs_posterior_bilateral.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dis = np.zeros((2,7700-5700))\n",
    "for i in range(2):\n",
    "    for j in np.round(tmp[i]).astype(int):\n",
    "        dis[i,j-5700] += 1\n",
    "#dis[0]/=np.sum(dis[0])\n",
    "#dis[1]/=np.sum(dis[1])\n",
    "from scipy.stats import mannwhitneyu\n",
    "t_stat, p_value = mannwhitneyu(dis[0], dis[1])\n",
    "p_value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "group_color = np.array([[120,214,230],[187,48,84]])/255\n",
    "tmp = [[],[]]\n",
    "for i in X:\n",
    "    g = np.argmin(np.sum((group_color-X[i]['color'])**2,axis=-1))\n",
    "    tmp[g] += X[i]['terminal']\n",
    "for i in range(2):\n",
    "    tmp[i] = np.array(tmp[i])[:,1]\n",
    "    #tmp[i] = np.where(tmp[i]<5700,11400-tmp[i],tmp[i])\n",
    "fig = plt.figure(figsize=(5,5))\n",
    "ax = fig.add_subplot(1,1,1)\n",
    "parts = ax.violinplot(tmp,showmeans=False,showextrema=False,showmedians=False,vert=False)\n",
    "for pc,c in zip(parts['bodies'],group_color):\n",
    "    pc.set_facecolor(c)\n",
    "    pc.set_edgecolor('black')\n",
    "    pc.set_alpha(1)\n",
    "ax.scatter([np.median(i) for i in tmp],range(1,len(tmp)+1),color='k',s=10)\n",
    "ax.plot([np.median(i) for i in tmp],range(1,len(tmp)+1),color='k',linestyle='--')\n",
    "ax.axvline(5700,color='k',linestyle='--')\n",
    "ax.set_yticks([1,2],['blue group','red group'],fontsize=fontsize_ticks)\n",
    "ax.set_xlabel('Distance from lateral\\n(rotated space \\u00B5m)',fontsize=fontsize_label)\n",
    "ax.set_ylabel('Modality',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.suptitle('Terminal distribution in lateral-medial axis',fontsize=fontsize_title)\n",
    "plt.savefig(medulla_path+'Terminal_in_reticular_MOs_sagittal_bilateral.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dis = np.zeros((2,7000-5000))\n",
    "for i in range(2):\n",
    "    for j in np.round(tmp[i]).astype(int):\n",
    "        dis[i,j-5000] += 1\n",
    "#dis[0]/=np.sum(dis[0])\n",
    "#dis[1]/=np.sum(dis[1])\n",
    "from scipy.stats import mannwhitneyu\n",
    "t_stat, p_value = mannwhitneyu(dis[0], dis[1])\n",
    "p_value"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Pons"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pons_path = brainstem_path+'pons/'\n",
    "create_dir(pons_path)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### axons"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if not os.path.exists(pons_path+'projection_axon_info.npy'):\n",
    "    mask = get_region_mask('P')\n",
    "    import random\n",
    "    _max = 200\n",
    "    color_transform = {'VIS':color_pool[3],'ACA':color_pool[4],'MOs':color_pool[0],'SSs':color_pool[1],'AUD':color_pool[2],'AI':color_pool[5],'SSp':color_pool[1],'MOp':color_pool[3],'TEa':color_pool[5],'PL':color_pool[7],'ORB':color_pool[6],'ILA':color_pool[8],'RSP':color_pool[9],}\n",
    "    info = {}\n",
    "    for neuron in tool_bar(cortex_neuron_name,desc='iteration on neurons (generating mapping colors) ...',leave=False,colour='#55AF7B'):\n",
    "        region = neuron_to_brainstem_information[neuron]['cortex_region']\n",
    "        if region not in info:\n",
    "            info[region] = {'neuron':[]}\n",
    "        info[region]['neuron'].append(neuron)\n",
    "    for region in info:\n",
    "        if region not in color_transform:\n",
    "            continue\n",
    "        info[region]['axon'] = get_terminal_lines_in_mask(info[region]['neuron'] if len(info[region]['neuron'])<=_max else random.sample(info[region]['neuron'],_max),mask,color=color_transform[region])\n",
    "    np.save(pons_path+'projection_axon_info.npy',info)\n",
    "else:\n",
    "    info = np.load(pons_path+'projection_axon_info.npy',allow_pickle=True).item()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear()\n",
    "for i,j in zip(['AUD','SSp','VIS','ACA','MOs','AI'],[2,1,3,0,4,5]):\n",
    "    draw_lines(neuronvis,info[i]['axon'][0],set_color(info[i]['axon'][1],color_pool[j]))\n",
    "    #draw_points(neuronvis,info[i]['axon'][2],set_color(info[i]['axon'][2],color_pool[j]))\n",
    "neuronvis.render.setPointSize(1)\n",
    "for i in ['PB']:\n",
    "    neuronvis.addRegion(i)\n",
    "neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### terminals"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mask = get_region_mask('P')\n",
    "X = {}\n",
    "for id,neuron in enumerate(neuron_to_brainstem_information):\n",
    "    region = cortical_module_transform[neuron_to_brainstem_information[neuron]['cortex_region']]\n",
    "    if region not in X:\n",
    "        X[region] = {'terminal':[]}\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            X[region]['terminal'].append(terminal.copy())\n",
    "            if neuron_terminals[neuron]['mirror_mark']:\n",
    "                X[region]['terminal'][-1][2] = 11400-X[region]['terminal'][-1][2]\n",
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "for i in X:\n",
    "    neuronvis.clear(root=True)\n",
    "    neuronvis.addRegion('P')\n",
    "    draw_points(neuronvis,X[i]['terminal'],[cortical_module_color[i].tolist() for j in X[i]['terminal']])\n",
    "    neuronvis.render.setPointSize(3)\n",
    "    for j in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "        set_view(neuronvis,j)\n",
    "        neuronvis.render.savepng(pons_path+'P_terminal_'+i+'_'+j+'.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### module and its target"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mask = get_region_mask('P')\n",
    "X = {}\n",
    "for id,neuron in enumerate(neuron_to_brainstem_information):\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']\n",
    "    module = cortical_module_transform[region]\n",
    "    if module not in X:\n",
    "        X[module] = {'terminal':[],'color':[]}\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            X[module]['terminal'].append(terminal.copy())\n",
    "            if neuron_terminals[neuron]['mirror_mark']:\n",
    "                X[module]['terminal'][-1][2] = 11400-X[module]['terminal'][-1][2]\n",
    "            X[module]['color'].append(cortical_module_color[module])\n",
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "for module in X:\n",
    "    neuronvis.clear()\n",
    "    neuronvis.addRegion('P')\n",
    "    draw_points(neuronvis,X[module]['terminal'],np.array(X[module]['color']).tolist())\n",
    "    neuronvis.render.setPointSize(1)\n",
    "    for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "        set_view(neuronvis,i)\n",
    "        neuronvis.render.savepng(pons_path+'terminal_'+module+'_'+i+'.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "neuronvis.addRegion('P')\n",
    "set_view(neuronvis,'posterior')\n",
    "neuronvis.render.savepng(pons_path+'P.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### ventral"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pons_ventral_path = pons_path+'ventral/'\n",
    "create_dir(pons_ventral_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mask = 0.0\n",
    "for i in ['PG','TRN']:\n",
    "    mask += get_region_mask(i)\n",
    "X = {}\n",
    "for id,neuron in enumerate(neuron_to_brainstem_information):\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']\n",
    "    module = cortical_module_transform[region]\n",
    "    if module not in X:\n",
    "        X[module] = {'terminal':[],'color':[]}\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            X[module]['terminal'].append(terminal.copy())\n",
    "            if neuron_terminals[neuron]['mirror_mark']:\n",
    "                X[module]['terminal'][-1][2] = 11400-X[module]['terminal'][-1][2]\n",
    "            X[module]['color'].append(cortical_module_color[module])\n",
    "background_cronal = (np.sum(mask,axis=0)>0).astype(float)\n",
    "background_sagittal = (np.sum(mask,axis=-1)>0).astype(float).T\n",
    "background_dorsal = (np.sum(mask,axis=1)>0).astype(float)\n",
    "for i in X:\n",
    "    if len(X[i]['terminal'])==0:\n",
    "        continue\n",
    "    X_cronal = np.array(X[i]['terminal'])[...,1:]/10\n",
    "    X_sagittal = np.array(X[i]['terminal'])[...,:2]/10\n",
    "    X_dorsal = np.concatenate([np.array(X[i]['terminal'])[:,:1],np.array(X[i]['terminal'])[:,2:]],axis=-1)/10\n",
    "    a = X_sagittal[:,0].copy()\n",
    "    X_sagittal[:,0] = X_sagittal[:,1]*1\n",
    "    X_sagittal[:,1] = a\n",
    "    custom_cmap = LinearSegmentedColormap.from_list('custom',[[1,1,1],cortical_module_color[i]])\n",
    "    plot_points_distribution_in_mask(X_cronal,background_cronal,title=i+'_cronal',save_path=pons_ventral_path+'distribution_'+i+'_cronal',cmap=custom_cmap)\n",
    "    plot_points_distribution_in_mask(X_sagittal,background_sagittal,title=i+'_sagittal',save_path=pons_ventral_path+'distribution_'+i+'_sagittal',cmap=custom_cmap)\n",
    "    plot_points_distribution_in_mask(X_dorsal,background_dorsal,title=i+'_dorsal',save_path=pons_ventral_path+'distribution_'+i+'_dorsal',cmap=custom_cmap)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "region_list = ['PG','TRN']\n",
    "X = {}\n",
    "for neuron in neuron_to_brainstem_information:\n",
    "    region = cortical_module_transform[neuron_to_brainstem_information[neuron]['cortex_region']]\n",
    "    if region not in X:\n",
    "        X[region] = []\n",
    "    X[region].append(neuron)\n",
    "for i in X:\n",
    "    X[i] = get_neuron_axon_info(X[i],terminal_or_length='terminal',if_combined=True,if_log=False,filter=region_list)[1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### PG vs TRN source"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_labels = ['SS','MO','Cingulate','VIS','Insula','PFC','AUD'][::-1]\n",
    "h = 0.25\n",
    "fig = plt.figure(figsize=(4,8))\n",
    "tmp = np.array([np.sum(X[i],axis=0) for i in x_labels])\n",
    "plt.barh(range(len(x_labels)),tmp[:,0],color=color_pool[0],height=h,label='PG')\n",
    "plt.barh([i-h for i in range(len(x_labels))],tmp[:,1],color=color_pool[3],height=h,label='TRN')\n",
    "#plt.barh(tmp[:,1],range(len(x_labels)),color=color_pool[3])\n",
    "#plt.xticks([0,250,500],[0,250,500],fontsize=fontsize_ticks)\n",
    "plt.yticks([i-h/2 for i in range(len(x_labels))],x_labels,fontsize=fontsize_ticks)\n",
    "plt.xlabel('Terminal number',fontsize=fontsize_label)\n",
    "plt.ylabel('Probability',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.title('Source compare',fontsize=fontsize_title)\n",
    "plt.legend()\n",
    "plt.savefig(pons_ventral_path+'PG_TRN_source.pdf',format='pdf')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_label = ['MO','SS','Limbic','VIS','Insula','PFC','AUD']\n",
    "a = {}\n",
    "for i,j in enumerate(x_label):\n",
    "    tmp = np.array(X[j])\n",
    "    a[j] = plt.hist(tmp[:,0],label=j,histtype='step',cumulative=True,density=True,range=(0,500),bins=200)\n",
    "    #plt.scatter(tmp[:,0]*0+i+0.2,tmp[:,1],label=j)\n",
    "#plt.xticks(range(len(x_label)),x_label)\n",
    "plt.legend()\n",
    "plt.show()\n",
    "from scipy.ndimage import gaussian_filter1d\n",
    "for i,k in zip(x_label,range(len(x_label))):\n",
    "    r = k/(len(x_label)-1)\n",
    "    j = a[i]\n",
    "    plt.plot(0.5*(j[1][1:]+j[1][:-1]),gaussian_filter1d(j[0],55),label=i,color=r*color_pool[3]+(1-r)*color_pool[0])\n",
    "plt.xticks([0,250,500],[0,250,500],fontsize=fontsize_ticks)\n",
    "plt.yticks([0.9,0.95,1],[0.9,0.95,1],fontsize=fontsize_ticks)\n",
    "plt.xlabel('Terminal number',fontsize=fontsize_label)\n",
    "plt.ylabel('Probability',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.title('SSp subarea in PG (cumulative curve)',fontsize=fontsize_title)\n",
    "plt.legend()\n",
    "plt.savefig(pons_ventral_path+'in_PG.pdf',format='pdf')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_label = ['MO','SS','Limbic','VIS','Insula','PFC','AUD']\n",
    "a = {}\n",
    "for i,j in enumerate(x_label):\n",
    "    tmp = np.array(X[j])\n",
    "    a[j] = plt.hist(tmp[:,1],label=j,histtype='step',cumulative=True,density=True,range=(0,500),bins=200)\n",
    "    #plt.scatter(tmp[:,0]*0+i+0.2,tmp[:,1],label=j)\n",
    "#plt.xticks(range(len(x_label)),x_label)\n",
    "plt.legend()\n",
    "plt.show()\n",
    "from scipy.ndimage import gaussian_filter1d\n",
    "for i,k in zip(x_label,range(len(x_label))):\n",
    "    r = k/(len(x_label)-1)\n",
    "    j = a[i]\n",
    "    plt.plot(0.5*(j[1][1:]+j[1][:-1]),gaussian_filter1d(j[0],55),label=i,color=r*color_pool[3]+(1-r)*color_pool[0])\n",
    "plt.xticks([0,250,500],[0,250,500],fontsize=fontsize_ticks)\n",
    "plt.yticks([0.9,0.95,1],[0.9,0.95,1],fontsize=fontsize_ticks)\n",
    "plt.xlabel('Terminal number',fontsize=fontsize_label)\n",
    "plt.ylabel('Probability',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.title('SSp subarea in TRN (cumulative curve)',fontsize=fontsize_title)\n",
    "plt.legend()\n",
    "plt.savefig(pons_ventral_path+'in_TRN.pdf',format='pdf')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### PG and TRN is more at body"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "subregion_transform = {'-m':'head','-n':'head','-bfd':'whisker','-ul':'body','-ll':'body','-tr':'body'}\n",
    "region_list = ['PG','TRN']\n",
    "X = {}\n",
    "for neuron in neuron_to_brainstem_information:\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']\n",
    "    subregion = neuron_to_brainstem_information[neuron]['cortex_subregion']\n",
    "    if region!='SSp':\n",
    "        continue\n",
    "    try:\n",
    "        t = subregion_transform[subregion]\n",
    "    except:\n",
    "        continue\n",
    "    if t not in X:\n",
    "        X[t] = {'neuron':[]}\n",
    "    X[t]['neuron'].append(neuron)\n",
    "def func(pct, allvals):\n",
    "    absolute = int(np.round(pct/100.*np.sum(allvals)))\n",
    "    return f\"{pct:.1f}%\\n({absolute:d})\"\n",
    "fig,axes = plt.subplots(1,3,figsize=(7,3))\n",
    "for index,i in enumerate(['body','whisker','head']):\n",
    "    X[i]['terminal'] = get_neuron_axon_info(X[i]['neuron'],terminal_or_length='terminal',if_combined=True,if_log=False,filter=region_list)[1]\n",
    "    tmp = np.mean(X[i]['terminal'],axis=0)\n",
    "    axes[index].pie(tmp,labels=region_list, autopct=lambda pct: func(pct,tmp),textprops=dict(color=\"w\"),colors=[color_pool[1],color_pool[3]])\n",
    "    axes[index].set_title(i,fontsize=fontsize_title/1.5)\n",
    "fig.suptitle('PG and TRN compare',fontsize=fontsize_title)\n",
    "plt.legend()\n",
    "plt.savefig(pons_ventral_path+'PG_TRN_difference.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### PG and TRN neurons"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "for i,j in zip([iondata.getNeuronListBySomaRegion('PG'),iondata.getNeuronListBySomaRegion('TRN')],[1,3]):\n",
    "    for k in i:\n",
    "        neuron_tree = iondata.getNeuronTreeByID(k['sampleid'],k['name'])\n",
    "        neuron_tree.somaRadius = 10\n",
    "        neuronvis.addNeuronTree(neuron_tree,axonColor=color_pool[j],somaColor=[0.5,0.5,0.5],dendriteColor=[0.5,0.5,0.5],mirrorToRight=True,isLine=False)\n",
    "neuronvis.render.savepng(pons_ventral_path+'PG_TRN_neuron_examples.png')\n",
    "for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "    set_view(neuronvis,i)\n",
    "    neuronvis.render.savepng(pons_ventral_path+'PG_TRN_neuron_examples_'+i+'.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### topology"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = ['PG']\n",
    "mask = 0.0\n",
    "for i,j in enumerate(target):\n",
    "    mask = np.where(get_region_mask(j),i+1,mask)\n",
    "X = {}\n",
    "for id,neuron in enumerate(neuron_to_brainstem_information):\n",
    "    region = cortical_module_transform[neuron_to_brainstem_information[neuron]['cortex_region']]\n",
    "    if region not in X:\n",
    "        X[region] = {'terminal':[]}\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            X[region]['terminal'].append(terminal.copy())\n",
    "            if neuron_terminals[neuron]['mirror_mark']:\n",
    "                X[region]['terminal'][-1][2] = 11400-X[region]['terminal'][-1][2]\n",
    "mask_2D = np.sum(mask,axis=0)\n",
    "a,b = get_value_range(mask_2D)\n",
    "tmp = [np.mean(a),np.mean(b)]\n",
    "tmp2 = np.max([a[1]-a[0],b[1]-b[0]])\n",
    "_r = [tmp[0]-tmp2/2,tmp[0]+tmp2/2,tmp[1]+tmp2/2,tmp[1]-tmp2/2]\n",
    "color_transform = {'PFC':0,'RSP':3}\n",
    "m = 1-get_contour(mask_2D>0)\n",
    "fig,ax = plt.subplots(figsize=(5,5))\n",
    "ax.matshow(m,cmap='gray')\n",
    "for i in X:\n",
    "    tmp = np.array(X[i]['terminal'])\n",
    "    ax.scatter(tmp[:,2]/10,tmp[:,1]/10,color=cortical_module_color[i],s=0.1,alpha=0.25)\n",
    "ax.set_xlim(_r[0],_r[1])\n",
    "ax.set_ylim(_r[2],_r[3])\n",
    "ax.set_title(\"Terminal in PG\",fontsize=fontsize_title)\n",
    "ax.set_axis_off()\n",
    "ax.legend(handles=[mpatches.Patch(color=cortical_module_color[i], label=i) for i in cortical_module_color],loc='lower right')\n",
    "plt.savefig(pons_ventral_path+'PG_topology.pdf',format='pdf',bbox_inches='tight',dpi=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure(figsize=(8,4))\n",
    "for i,j in enumerate(X):\n",
    "    ax = fig.add_subplot(2,4,i+1)\n",
    "    ax.matshow(m,cmap='gray')\n",
    "    tmp = np.array(X[j]['terminal'])\n",
    "    ax.scatter(tmp[:,2]/10,tmp[:,1]/10,color=cortical_module_color[j],s=0.05,alpha=0.25)\n",
    "    ax.set_xlim(_r[0],_r[1])\n",
    "    ax.set_ylim(_r[2],_r[3])\n",
    "    ax.set_axis_off()\n",
    "    ax.set_title(j,fontsize=fontsize_title*0.8)\n",
    "#fig.suptitle('Terminals in PG',fontsize=fontsize_title)\n",
    "plt.savefig(pons_ventral_path+'PG_topology.pdf',format='pdf',bbox_inches='tight',dpi=100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### lateral"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pons_lateral_path = pons_path+'lateral/'\n",
    "create_dir(pons_lateral_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "index_list = {'trigeminal':[brainstem_region.index(i) for i in ['PSV']],'PB':[brainstem_region.index(i) for i in ['PB']],'auditory':[brainstem_region.index(i) for i in ['NLL','SOC']],'PB':[brainstem_region.index(i) for i in ['PB']]}\n",
    "color_transform = {'PB':5,'auditory':2,'trigeminal':1}\n",
    "info = {}\n",
    "for i in neuron_to_brainstem_information:\n",
    "    d = neuron_to_brainstem_information[i]['soma_depth']\n",
    "    p = neuron_to_brainstem_information[i]['soma_flatten_position']\n",
    "    info[i] = [d,p]\n",
    "    for j in index_list:\n",
    "        tmp = 0.0\n",
    "        for k in index_list[j]:\n",
    "            tmp += neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][k]\n",
    "        info[i].append(tmp)\n",
    "result,soma_list = get_pdf(_flatmap[...,0],neuron_to_brainstem_information,info)\n",
    "a = _flatmap*0.0\n",
    "b = []\n",
    "for i,j in zip(index_list,result):\n",
    "    c = np.expand_dims(j/np.max(j),axis=-1)\n",
    "    b.append([c,c*color_pool[color_transform[i]]])\n",
    "c = np.sum([i[0] for i in b],axis=0)\n",
    "c = np.where(c==0.0,1.0,c)\n",
    "merged_result = 0.0\n",
    "for i in b:\n",
    "    merged_result += i[0]/c*i[1]\n",
    "d = np.max([i[0] for i in b],axis=0)\n",
    "tmp = merged_result+(1-d)*np.array([1.0,1.0,1.0])\n",
    "for i in soma_list:\n",
    "    for k,l in zip(i[0],i[1]):\n",
    "        cv.circle(tmp,[int(np.round(k[1])), int(np.round(k[0]))],int(np.round(l*10))+1,np.array([0.5,0.5,0.5]),-1)\n",
    "fig,ax = plt.subplots(figsize=(5,5))\n",
    "ax.imshow(tmp,zorder=0)\n",
    "ax.imshow(flatmap_mask,zorder=1)\n",
    "ax.imshow(flatmap,zorder=2)\n",
    "ax.set_xlim(flatten_map_r[0],flatten_map_r[1])\n",
    "ax.set_ylim(flatten_map_r[2],flatten_map_r[3])\n",
    "ax.set_title('Lateral part of pons',color='k',fontsize=fontsize_title)\n",
    "ax.set_axis_off()\n",
    "ax.legend(handles=[mpatches.Patch(color=color_pool[color_transform[i]], label=i) for i in color_transform],loc='lower right')\n",
    "plt.savefig(pons_lateral_path+'region_projection_to_lateral_part.pdf',format='pdf',bbox_inches='tight',dpi=100)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### lateral visualization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = ['SOC','NLL','PB','PSV']\n",
    "mask = 0.0\n",
    "for i in target:\n",
    "    mask += get_region_mask(i)\n",
    "X = {}\n",
    "for id,neuron in enumerate(neuron_to_brainstem_information):\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']\n",
    "    if region not in ['AUD','TEa','PERI','ECT','AI','VISC','GU','SSs','SSp','MOp','MOs']:\n",
    "        continue\n",
    "    if region not in X:\n",
    "        X[region] = {'terminal':[]}\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            X[region]['terminal'].append(terminal.copy())\n",
    "            if neuron_terminals[neuron]['mirror_mark']:\n",
    "                X[region]['terminal'][-1][2] = 11400-X[region]['terminal'][-1][2]\n",
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "for i in target:\n",
    "    neuronvis.addRegion(i)\n",
    "for i in ['AUD','TEa','PERI','ECT']:\n",
    "    draw_points(neuronvis,X[i]['terminal'],[color_pool[2].tolist() for j in X[i]['terminal']])\n",
    "for i in ['AI','VISC','GU']:\n",
    "    draw_points(neuronvis,X[i]['terminal'],[color_pool[5].tolist() for j in X[i]['terminal']])\n",
    "for i in ['SSs','SSp','MOp','MOs']:\n",
    "    draw_points(neuronvis,X[i]['terminal'],[color_pool[1].tolist() for j in X[i]['terminal']])\n",
    "neuronvis.render.setPointSize(3)\n",
    "for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "    set_view(neuronvis,i)\n",
    "    neuronvis.render.savepng(pons_lateral_path+'sensory_terminal_'+i+'.png')\n",
    "neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### three sensory modalities merge into PB"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = ['SOC','NLL','PSV','PB']\n",
    "info = {}\n",
    "for id,neuron in enumerate(neuron_to_brainstem_information):\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']\n",
    "    if region not in ['AUD','TEa','PERI','ECT','AI','VISC','GU','SSs','SSp','MOp','MOs']:\n",
    "        continue\n",
    "    if region not in info:\n",
    "        info[region] = []\n",
    "    info[region].append(neuron)\n",
    "for i in info:\n",
    "    info[i] = get_neuron_axon_info(info[i],terminal_or_length='terminal',if_combined=True,if_log=False,filter=target)[1]\n",
    "for i in info:\n",
    "    info[i] = [len(info[i]),np.sum(info[i],axis=0)]\n",
    "    info[i][1][1] += info[i][1][0]\n",
    "    info[i][1] = info[i][1][1:]\n",
    "_transform = {'Temporal':['AUD','TEa','PERI','ECT'],'SS&MO':['SSs','SSp','MOp','MOs'],'Insular':['AI','VISC','GU']}\n",
    "X = [[],[],[]]\n",
    "for i in _transform:\n",
    "    X[0].append(0)\n",
    "    X[1].append(0)\n",
    "    X[2].append(0)\n",
    "    for j in _transform[i]:\n",
    "        X[0][-1] += info[j][0]\n",
    "        X[1][-1] += np.sum(info[j][1])\n",
    "        X[2][-1] += info[j][1]\n",
    "    X[2][-1] = X[2][-1]/np.linalg.norm(X[2][-1])\n",
    "X[2] = np.array(X[2])\n",
    "fig = plt.figure(figsize=(2,5))\n",
    "plt.matshow(X[2],vmin=0,vmax=1,cmap='BuGn')\n",
    "_ = plt.xticks(range(3),['Auditory','Trigeminal','PB'],fontsize=fontsize_ticks*0.8)\n",
    "_ = plt.yticks(range(3),_transform.keys(),fontsize=fontsize_ticks*0.8)\n",
    "plt.xlabel('Target',fontsize=fontsize_label)\n",
    "plt.ylabel('Source',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.title('Sensory collumn pattern',fontsize=fontsize_title)\n",
    "plt.colorbar()\n",
    "plt.savefig(pons_lateral_path+'Sensory_integration_matrix.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = [[],[],[]]\n",
    "for i in _transform:\n",
    "    X[0].append(0)\n",
    "    X[1].append(0)\n",
    "    X[2].append(0)\n",
    "    for j in _transform[i]:\n",
    "        X[0][-1] += info[j][0]\n",
    "        X[1][-1] += np.sum(info[j][1])\n",
    "        X[2][-1] += info[j][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "X[2], np.sum(X[2],axis=-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.round(X[2]/np.sum(X[2],axis=-1,keepdims=True)*100,0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### bilateral"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "source = ['AUD','TEa','PERI','ECT','AI','VISC','GU','SSs','SSp','MOp','MOs']\n",
    "target = ['SOC','NLL','PSV','PB']\n",
    "mask = 0.0\n",
    "for i,j in enumerate(target):\n",
    "    mask = np.where(get_region_mask(j),i+1,mask)\n",
    "X = {}\n",
    "for id,neuron in enumerate(neuron_to_brainstem_information):\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']\n",
    "    if region not in source:\n",
    "        continue\n",
    "    if region not in X:\n",
    "        X[region] = {i:[] for i in target}\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        v = int(get_mask_value(mask,np.array(terminal)/10))\n",
    "        if v>0:\n",
    "            X[region][target[v-1]].append(terminal.copy())\n",
    "            if neuron_terminals[neuron]['mirror_mark']:\n",
    "                X[region][target[v-1]][-1][2] = 11400-X[region][target[v-1]][-1][2]\n",
    "_transform = {'Temporal':['AUD','TEa','PERI','ECT'],'SS&MO':['SSs','SSp','MOp','MOs'],'Insular':['AI','VISC','GU']}\n",
    "tmp = {}\n",
    "for i in _transform:\n",
    "    tmp[i] = []\n",
    "    for j in _transform[i]:\n",
    "        for k in X[j]:\n",
    "            tmp[i] += X[j][k]\n",
    "fig = plt.figure(figsize=(5,5))\n",
    "ax = fig.add_subplot(1,1,1)\n",
    "parts = ax.violinplot([np.array(tmp[i])[:,2] for i in tmp],showmeans=False,showextrema=False,showmedians=False,vert=False)\n",
    "for pc,c in zip(parts['bodies'],[2,1,5]):\n",
    "    pc.set_facecolor(color_pool[c])\n",
    "    pc.set_edgecolor('black')\n",
    "    pc.set_alpha(1)\n",
    "ax.axvline(5700,color='k',linestyle='--')\n",
    "ax.invert_yaxis()\n",
    "ax.set_xticks([5700-2500,5700,5700+2500],['ipsilateral\\n(3200)','midline\\n(5700)','contralatel\\n(8200)'],fontsize=fontsize_ticks)\n",
    "ax.set_yticks([1,2,3],['Auditory','Trigeminal','PB'],fontsize=fontsize_ticks)\n",
    "ax.set_xlabel('Distance from lateral (\\u00B5m)',fontsize=fontsize_label)\n",
    "ax.set_ylabel('Target',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "ax.set_title('Terminal distribution in sensory column',fontsize=fontsize_title)\n",
    "color_transform = {'Temporal':2,'SS&MO':1,'Insular':5}\n",
    "ax.legend(handles=[mpatches.Patch(color=color_pool[color_transform[i]], label=i) for i in color_transform],loc='upper right')\n",
    "plt.savefig(pons_lateral_path+'Sensory_bilateral_statistics.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### single neuron visualization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "for i,j in zip(['221243165.swc','220198264.swc'],[0,1]):\n",
    "    neuronvis.clear()\n",
    "    neuronvis.addNeuronByID(i[:-7],i[-7:],color_pool[j],mirrorToRight=True,somaHide=False,dendriteHide=True,isLine=False)\n",
    "    neuronvis.addRegion('PB',[0,0,1])\n",
    "    set_view(neuronvis,'anterior')\n",
    "    neuronvis.render.savepng(pons_lateral_path+i[:-4]+'.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(neuron_to_brainstem_information['220198264.swc']['cortex_region'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### medial"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pons_medial_path = pons_path+'medial/'\n",
    "create_dir(pons_medial_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "index_list = {'PRN':[brainstem_region.index(i) for i in ['PRNr','PRNc','V','P5','SUT','I5']]}\n",
    "color_transform = {'PRN':0}\n",
    "info = {}\n",
    "for i in neuron_to_brainstem_information:\n",
    "    d = neuron_to_brainstem_information[i]['soma_depth']\n",
    "    p = neuron_to_brainstem_information[i]['soma_flatten_position']\n",
    "    info[i] = [d,p]\n",
    "    for j in index_list:\n",
    "        tmp = 0.0\n",
    "        for k in index_list[j]:\n",
    "            tmp += neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][k]\n",
    "        info[i].append(tmp)\n",
    "result,soma_list = get_pdf(_flatmap[...,0],neuron_to_brainstem_information,info)\n",
    "a = _flatmap*0.0\n",
    "b = []\n",
    "for i,j in zip(index_list,result):\n",
    "    c = np.expand_dims(j/np.max(j),axis=-1)\n",
    "    b.append([c,c*color_pool[color_transform[i]]])\n",
    "c = np.sum([i[0] for i in b],axis=0)\n",
    "c = np.where(c==0.0,1.0,c)\n",
    "merged_result = 0.0\n",
    "for i in b:\n",
    "    merged_result += i[0]/c*i[1]\n",
    "d = np.max([i[0] for i in b],axis=0)\n",
    "tmp = merged_result+(1-d)*np.array([1.0,1.0,1.0])\n",
    "for i in soma_list:\n",
    "    for k,l in zip(i[0],i[1]):\n",
    "        cv.circle(tmp,[int(np.round(k[1])), int(np.round(k[0]))],int(np.round(l*10))+1,np.array([0.5,0.5,0.5]),-1)\n",
    "fig,ax = plt.subplots(figsize=(5,5))\n",
    "ax.imshow(tmp,zorder=0)\n",
    "ax.imshow(flatmap_mask,zorder=1)\n",
    "ax.imshow(flatmap,zorder=2)\n",
    "ax.set_xlim(flatten_map_r[0],flatten_map_r[1])\n",
    "ax.set_ylim(flatten_map_r[2],flatten_map_r[3])\n",
    "ax.set_title('Medial part of pons',color='k',fontsize=fontsize_title)\n",
    "ax.set_axis_off()\n",
    "ax.legend(handles=[mpatches.Patch(color=color_pool[color_transform[i]], label=i) for i in color_transform],loc='lower right')\n",
    "plt.savefig(pons_medial_path+'region_projection_to_medial_part.pdf',format='pdf',bbox_inches='tight',dpi=100)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### MOs and ACAd difference"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = ['PRNr','PRNc','V','SUT','P5','I5']\n",
    "info = {}\n",
    "for neuron in tool_bar(cortex_neuron_name,desc='iteration on neurons (generating mapping colors) ...',leave=False,colour='#55AF7B'):\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']+neuron_to_brainstem_information[neuron]['cortex_subregion']\n",
    "    if region not in ['MOs','ACAd']:\n",
    "        continue\n",
    "    if region not in info:\n",
    "        info[region] = []\n",
    "    info[region].append(neuron)\n",
    "for i in info:\n",
    "    info[i] = get_neuron_axon_info(info[i],terminal_or_length='terminal',if_combined=True,if_log=False,filter=target)[1]\n",
    "    info[i] = [len(info[i]),np.sum(info[i]),np.sum(info[i],axis=0)]\n",
    "width = 0.25\n",
    "scale = 1.1\n",
    "plt.figure(figsize=(4,5))\n",
    "for i,j,k in zip(['MOs','ACAd'],[0,3],[0,1]):\n",
    "    x = np.array(info[i][2]).astype(float)\n",
    "    x /= np.sum(x)\n",
    "    plt.bar([l+width*1.1*k for l in range(len(target))],x*100,width=0.25,color=color_pool[j],label=i)\n",
    "plt.xticks(range(len(target)),target,fontsize=fontsize_ticks)\n",
    "plt.xlabel('Target',fontsize=fontsize_label)\n",
    "plt.ylabel('Ratio (%)',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.title(\"Meidal projection\",fontsize=fontsize_title)\n",
    "plt.legend()\n",
    "plt.savefig(pons_medial_path+'medial_projection.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()\n",
    "info"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### motor nuclei projection terminal visualization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = ['V','SUT','P5']\n",
    "mask = 0.0\n",
    "for i in target:\n",
    "    mask += get_region_mask(i)\n",
    "X = {}\n",
    "for id,neuron in enumerate(neuron_to_brainstem_information):\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']+neuron_to_brainstem_information[neuron]['cortex_subregion']\n",
    "    if region not in ['MOs','ACAd']:\n",
    "        continue\n",
    "    if region not in X:\n",
    "        X[region] = {'terminal':[]}\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            X[region]['terminal'].append(terminal.copy())\n",
    "            if neuron_terminals[neuron]['mirror_mark']:\n",
    "                X[region]['terminal'][-1][2] = 11400-X[region]['terminal'][-1][2]\n",
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "for i in target:\n",
    "    neuronvis.addRegion(i)\n",
    "for i in ['MOs']:\n",
    "    draw_points(neuronvis,X[i]['terminal'],[color_pool[0].tolist() for j in X[i]['terminal']])\n",
    "for i in ['ACAd']:\n",
    "    draw_points(neuronvis,X[i]['terminal'],[color_pool[3].tolist() for j in X[i]['terminal']])\n",
    "neuronvis.render.setPointSize(3)\n",
    "for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "    set_view(neuronvis,i)\n",
    "    neuronvis.render.savepng(pons_medial_path+'motor_terminal_'+i+'.png')\n",
    "neuronvis.render.closeWindow()\n",
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "for i in target:\n",
    "    neuronvis.addRegion(i)\n",
    "for i in ['MOs']:\n",
    "    draw_points(neuronvis,X[i]['terminal'],[color_pool[0].tolist() for j in X[i]['terminal']])\n",
    "neuronvis.render.setPointSize(3)\n",
    "for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "    set_view(neuronvis,i)\n",
    "    neuronvis.render.savepng(pons_medial_path+'motor_terminal_MOs_'+i+'.png')\n",
    "neuronvis.render.closeWindow()\n",
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "for i in target:\n",
    "    neuronvis.addRegion(i)\n",
    "for i in ['ACAd']:\n",
    "    draw_points(neuronvis,X[i]['terminal'],[color_pool[3].tolist() for j in X[i]['terminal']])\n",
    "neuronvis.render.setPointSize(3)\n",
    "for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "    set_view(neuronvis,i)\n",
    "    neuronvis.render.savepng(pons_medial_path+'motor_terminal_ACA_'+i+'.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### reticular formation projection terminal visualization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = ['PRNr']\n",
    "mask = 0.0\n",
    "for i in target:\n",
    "    mask += get_region_mask(i)\n",
    "X = {}\n",
    "for id,neuron in enumerate(neuron_to_brainstem_information):\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']+neuron_to_brainstem_information[neuron]['cortex_subregion']\n",
    "    if region not in ['MOs','ACAd']:\n",
    "        continue\n",
    "    if region not in X:\n",
    "        X[region] = {'terminal':[]}\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            X[region]['terminal'].append(terminal.copy())\n",
    "            if neuron_terminals[neuron]['mirror_mark']:\n",
    "                X[region]['terminal'][-1][2] = 11400-X[region]['terminal'][-1][2]\n",
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "for i in target:\n",
    "    neuronvis.addRegion(i)\n",
    "for i in ['MOs']:\n",
    "    draw_points(neuronvis,X[i]['terminal'],[color_pool[0].tolist() for j in X[i]['terminal']])\n",
    "for i in ['ACAd']:\n",
    "    draw_points(neuronvis,X[i]['terminal'],[color_pool[3].tolist() for j in X[i]['terminal']])\n",
    "neuronvis.render.setPointSize(3)\n",
    "for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "    set_view(neuronvis,i)\n",
    "    neuronvis.render.savepng(pons_medial_path+'RF_r_terminal_'+i+'.png')\n",
    "neuronvis.render.closeWindow()\n",
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "for i in target:\n",
    "    neuronvis.addRegion(i)\n",
    "for i in ['MOs']:\n",
    "    draw_points(neuronvis,X[i]['terminal'],[color_pool[0].tolist() for j in X[i]['terminal']])\n",
    "neuronvis.render.setPointSize(3)\n",
    "for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "    set_view(neuronvis,i)\n",
    "    neuronvis.render.savepng(pons_medial_path+'RF_r_terminal_MOs_'+i+'.png')\n",
    "neuronvis.render.closeWindow()\n",
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "for i in target:\n",
    "    neuronvis.addRegion(i)\n",
    "for i in ['ACAd']:\n",
    "    draw_points(neuronvis,X[i]['terminal'],[color_pool[3].tolist() for j in X[i]['terminal']])\n",
    "neuronvis.render.setPointSize(3)\n",
    "for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "    set_view(neuronvis,i)\n",
    "    neuronvis.render.savepng(pons_medial_path+'RF_r_terminal_ACA_'+i+'.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = ['PRNc']\n",
    "mask = 0.0\n",
    "for i in target:\n",
    "    mask += get_region_mask(i)\n",
    "X = {}\n",
    "for id,neuron in enumerate(neuron_to_brainstem_information):\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']+neuron_to_brainstem_information[neuron]['cortex_subregion']\n",
    "    if region not in ['MOs','ACAd']:\n",
    "        continue\n",
    "    if region not in X:\n",
    "        X[region] = {'terminal':[]}\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            X[region]['terminal'].append(terminal.copy())\n",
    "            if neuron_terminals[neuron]['mirror_mark']:\n",
    "                X[region]['terminal'][-1][2] = 11400-X[region]['terminal'][-1][2]\n",
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "for i in target:\n",
    "    neuronvis.addRegion(i)\n",
    "for i in ['MOs']:\n",
    "    draw_points(neuronvis,X[i]['terminal'],[color_pool[0].tolist() for j in X[i]['terminal']])\n",
    "for i in ['ACAd']:\n",
    "    draw_points(neuronvis,X[i]['terminal'],[color_pool[3].tolist() for j in X[i]['terminal']])\n",
    "neuronvis.render.setPointSize(3)\n",
    "for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "    set_view(neuronvis,i)\n",
    "    neuronvis.render.savepng(pons_medial_path+'RF_c_terminal_'+i+'.png')\n",
    "neuronvis.render.closeWindow()\n",
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "for i in target:\n",
    "    neuronvis.addRegion(i)\n",
    "for i in ['MOs']:\n",
    "    draw_points(neuronvis,X[i]['terminal'],[color_pool[0].tolist() for j in X[i]['terminal']])\n",
    "neuronvis.render.setPointSize(3)\n",
    "for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "    set_view(neuronvis,i)\n",
    "    neuronvis.render.savepng(pons_medial_path+'RF_c_terminal_MOs_'+i+'.png')\n",
    "neuronvis.render.closeWindow()\n",
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "for i in target:\n",
    "    neuronvis.addRegion(i)\n",
    "for i in ['ACAd']:\n",
    "    draw_points(neuronvis,X[i]['terminal'],[color_pool[3].tolist() for j in X[i]['terminal']])\n",
    "neuronvis.render.setPointSize(3)\n",
    "for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "    set_view(neuronvis,i)\n",
    "    neuronvis.render.savepng(pons_medial_path+'RF_c_terminal_ACA_'+i+'.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###### SCm neuron comparison"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp = {}\n",
    "for i in [1,2,3]:\n",
    "    tmp[i] = []\n",
    "    for k in Scene.scene2List(data_path+'SCm_'+str(i)+'.nv'):\n",
    "        tmp[i].append(k['sampleid']+k['name'],)\n",
    "SCm_neuron_terminals = {}\n",
    "for i in tmp:\n",
    "    for neuron in tmp[i]:\n",
    "        control_console_output(0)\n",
    "        neuron_tree = get_neuron_tree(neuron)\n",
    "        control_console_output(1)\n",
    "        SCm_neuron_terminals[neuron] = {'mirror_mark':neuron_tree.root.z>5700,'terminals':[]}\n",
    "        for terminal in neuron_tree.terminals:\n",
    "            SCm_neuron_terminals[neuron]['terminals'].append(terminal.xyz)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = ['PRNr']\n",
    "mask = 0.0\n",
    "for i in target:\n",
    "    mask += get_region_mask(i)\n",
    "X = {}\n",
    "for i in tmp:\n",
    "    if i not in X:\n",
    "        X[i] = {'terminal':[]}\n",
    "    for neuron in tmp[i]:\n",
    "        for terminal in SCm_neuron_terminals[neuron]['terminals']:\n",
    "            if if_inside_mask(mask,np.array(terminal)/10):\n",
    "                X[i]['terminal'].append(terminal.copy())\n",
    "                if SCm_neuron_terminals[neuron]['mirror_mark']:\n",
    "                    X[i]['terminal'][-1][2] = 11400-X[i]['terminal'][-1][2]\n",
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "for i in tmp:\n",
    "    neuronvis.clear(root=True)\n",
    "    for r in target:\n",
    "        neuronvis.addRegion(r)\n",
    "    draw_points(neuronvis,X[i]['terminal'],[color_pool[i].tolist() for j in X[i]['terminal']])\n",
    "    neuronvis.render.setPointSize(3)\n",
    "    for j in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "        set_view(neuronvis,j)\n",
    "        neuronvis.render.savepng(pons_medial_path+'RF_r_terminal_SCm_'+str(i)+'_'+j+'.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = ['PRNc']\n",
    "mask = 0.0\n",
    "for i in target:\n",
    "    mask += get_region_mask(i)\n",
    "X = {}\n",
    "for i in tmp:\n",
    "    if i not in X:\n",
    "        X[i] = {'terminal':[]}\n",
    "    for neuron in tmp[i]:\n",
    "        for terminal in SCm_neuron_terminals[neuron]['terminals']:\n",
    "            if if_inside_mask(mask,np.array(terminal)/10):\n",
    "                X[i]['terminal'].append(terminal.copy())\n",
    "                if SCm_neuron_terminals[neuron]['mirror_mark']:\n",
    "                    X[i]['terminal'][-1][2] = 11400-X[i]['terminal'][-1][2]\n",
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "for i in tmp:\n",
    "    neuronvis.clear(root=True)\n",
    "    for r in target:\n",
    "        neuronvis.addRegion(r)\n",
    "    draw_points(neuronvis,X[i]['terminal'],[color_pool[i].tolist() for j in X[i]['terminal']])\n",
    "    neuronvis.render.setPointSize(3)\n",
    "    for j in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "        set_view(neuronvis,j)\n",
    "        neuronvis.render.savepng(pons_medial_path+'RF_c_terminal_SCm_'+str(i)+'_'+j+'.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### reticular formation both side differences"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "source = ['MOs','ACAd']\n",
    "target = ['PRNr','PRNc']\n",
    "mask = 0.0\n",
    "for i,j in enumerate(target):\n",
    "    mask = np.where(get_region_mask(j),i+1,mask)\n",
    "X = {}\n",
    "for id,neuron in enumerate(neuron_to_brainstem_information):\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']+neuron_to_brainstem_information[neuron]['cortex_subregion']\n",
    "    if region not in source:\n",
    "        continue\n",
    "    if region not in X:\n",
    "        X[region] = {i:[] for i in target}\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        v = int(get_mask_value(mask,np.array(terminal)/10))\n",
    "        if v>0:\n",
    "            X[region][target[v-1]].append(terminal.copy())\n",
    "            if neuron_terminals[neuron]['mirror_mark']:\n",
    "                X[region][target[v-1]][-1][2] = 11400-X[region][target[v-1]][-1][2]\n",
    "fig = plt.figure(figsize=(5,5))\n",
    "ax = fig.add_subplot(1,1,1)\n",
    "tmp = [[np.array(X[i]['PRNr'])[:,2],np.array(X[i]['PRNc'])[:,2]] for i in source]\n",
    "tmp = tmp[0]+tmp[1]\n",
    "tmp = [tmp[i] for i in [0,2,1,3]]\n",
    "parts = ax.violinplot(tmp,showmeans=False,showextrema=False,showmedians=False,vert=False)\n",
    "for pc,c in zip(parts['bodies'],[0,3,0,3]):\n",
    "    pc.set_facecolor(color_pool[c])\n",
    "    pc.set_edgecolor('black')\n",
    "    pc.set_alpha(1)\n",
    "#ax.scatter(range(1,len(tmp)+1),[np.median(i) for i in tmp],color='k',s=10)\n",
    "#ax.plot(range(1,len(tmp)+1),[np.median(i) for i in tmp],color='k',linestyle='--')\n",
    "ax.axvline(5700,color='k',linestyle='--')\n",
    "ax.invert_yaxis()\n",
    "ax.set_xticks([5700-1500,5700,5700+1500],['ipsilateral\\n(4200)','midline\\n(5700)','contralatel\\n(7200)'],fontsize=fontsize_ticks)\n",
    "ax.set_yticks([1.5,3.5],['PRNr','PRNc'],fontsize=fontsize_ticks)\n",
    "ax.set_xlabel('Distance from lateral (\\u00B5m)',fontsize=fontsize_label)\n",
    "ax.set_ylabel('Target',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "ax.set_title('Terminal distribution in reticular',fontsize=fontsize_title)\n",
    "color_transform = {'MOs':0,'ACAd':3}\n",
    "ax.legend(handles=[mpatches.Patch(color=color_pool[color_transform[i]], label=i) for i in color_transform],loc='upper right')\n",
    "plt.savefig(pons_medial_path+'RF_bilateral_statistics.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### reticular examples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear()\n",
    "for i in ['PRNr','PRNc']:\n",
    "    neuronvis.addRegion(i)\n",
    "for i,j in zip(['220190017.swc','221225184.swc'],[0,3]):\n",
    "    neuron_tree = iondata.getNeuronTreeByID(i[:-7],i[-7:])\n",
    "    neuron_tree.somaRadius = 25\n",
    "    neuronvis.addNeuronTree(neuron_tree,axonColor=color_pool[j],somaColor=[0.5,0.5,0.5],dendriteColor=[0.5,0.5,0.5],mirrorToRight=True,isLine=False)\n",
    "set_view(neuronvis,'dorsal')\n",
    "neuronvis.render.savepng(pons_medial_path+'RF_example.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### central gray"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pons_central_path = pons_path+'central/'\n",
    "create_dir(pons_central_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "index_list = {'Lateral':[brainstem_region.index(i) for i in ['LDT','SLC','SLD']],'Medial':[brainstem_region.index(i) for i in ['NI','SG','DTN','PDTg']]}\n",
    "color_transform = {'Lateral':0,'Medial':3}\n",
    "info = {}\n",
    "for i in neuron_to_brainstem_information:\n",
    "    d = neuron_to_brainstem_information[i]['soma_depth']\n",
    "    p = neuron_to_brainstem_information[i]['soma_flatten_position']\n",
    "    info[i] = [d,p]\n",
    "    for j in index_list:\n",
    "        tmp = 0.0\n",
    "        for k in index_list[j]:\n",
    "            tmp += neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][k]\n",
    "        info[i].append(tmp)\n",
    "result,soma_list = get_pdf(_flatmap[...,0],neuron_to_brainstem_information,info)\n",
    "a = _flatmap*0.0\n",
    "b = []\n",
    "for i,j in zip(index_list,result):\n",
    "    c = np.expand_dims(j/np.max(j),axis=-1)\n",
    "    b.append([c,c*color_pool[color_transform[i]]])\n",
    "c = np.sum([i[0] for i in b],axis=0)\n",
    "c = np.where(c==0.0,1.0,c)\n",
    "merged_result = 0.0\n",
    "for i in b:\n",
    "    merged_result += i[0]/c*i[1]\n",
    "d = np.max([i[0] for i in b],axis=0)\n",
    "tmp = merged_result+(1-d)*np.array([1.0,1.0,1.0])\n",
    "for i in soma_list:\n",
    "    for k,l in zip(i[0],i[1]):\n",
    "        cv.circle(tmp,[int(np.round(k[1])), int(np.round(k[0]))],int(np.round(l*10))+1,np.array([0.5,0.5,0.5]),-1)\n",
    "fig,ax = plt.subplots(figsize=(5,5))\n",
    "ax.imshow(tmp,zorder=0)\n",
    "ax.imshow(flatmap_mask,zorder=1)\n",
    "ax.imshow(flatmap,zorder=2)\n",
    "ax.set_xlim(flatten_map_r[0],flatten_map_r[1])\n",
    "ax.set_ylim(flatten_map_r[2],flatten_map_r[3])\n",
    "ax.set_title('Central gray part of pons',color='k',fontsize=fontsize_title)\n",
    "ax.set_axis_off()\n",
    "ax.legend(handles=[mpatches.Patch(color=color_pool[color_transform[i]], label=i) for i in color_transform],loc='lower right')\n",
    "plt.savefig(pons_central_path+'region_projection_to_central_part.pdf',format='pdf',bbox_inches='tight',dpi=100)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### PFC->LDT and RSP->SG"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = ['PCG','SLC','SLD','LDT','DTN','PDTg','NI','SG']\n",
    "info = {}\n",
    "for neuron in tool_bar(cortex_neuron_name,desc='iteration on neurons (generating mapping colors) ...',leave=False,colour='#55AF7B'):\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']\n",
    "    if region in ['FRP','PL','ILA','ORB']:\n",
    "        region = 'PFC'\n",
    "    if region not in ['PFC','RSP']:\n",
    "        continue\n",
    "    if region not in info:\n",
    "        info[region] = []\n",
    "    info[region].append(neuron)\n",
    "for i in info:\n",
    "    info[i] = get_neuron_axon_info(info[i],terminal_or_length='terminal',if_combined=True,if_log=False,filter=target)[1]\n",
    "    info[i] = [len(info[i]),np.sum(info[i]),np.sum(info[i],axis=0)]\n",
    "width = 0.25\n",
    "scale = 1.1\n",
    "plt.figure(figsize=(5,5))\n",
    "for i,j,k in zip(['PFC','RSP'],[0,3],[0,1]):\n",
    "    x = np.array(info[i][2]).astype(float)\n",
    "    x /= np.sum(x)\n",
    "    plt.bar([l+width*1.1*k for l in range(len(target))],x*100,width=0.25,color=color_pool[j],label=i)\n",
    "plt.xticks(range(len(target)),target,fontsize=fontsize_ticks*0.75)\n",
    "plt.xlabel('Target',fontsize=fontsize_label)\n",
    "plt.ylabel('Ratio (%)',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.title(\"Central gray projection\",fontsize=fontsize_title)\n",
    "plt.legend()\n",
    "plt.savefig(pons_central_path+'Terminal_in_central_gray_statistics.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()\n",
    "info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = ['PCG','LDT','LC','SLC','SLD','NI','SG','DTN','PDTg']\n",
    "mask = 0.0\n",
    "for i,j in enumerate(target):\n",
    "    mask = np.where(get_region_mask(j),i+1,mask)\n",
    "X = {}\n",
    "for id,neuron in enumerate(neuron_to_brainstem_information):\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']\n",
    "    if region in ['FRP','PL','ILA','ORB']:\n",
    "        region = 'PFC'\n",
    "    if region not in ['PFC','RSP']:\n",
    "        continue\n",
    "    if region not in X:\n",
    "        X[region] = {'terminal':[]}\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            X[region]['terminal'].append(terminal.copy())\n",
    "            if neuron_terminals[neuron]['mirror_mark']:\n",
    "                X[region]['terminal'][-1][2] = 11400-X[region]['terminal'][-1][2]\n",
    "mask_2D = np.sum(mask,axis=0)\n",
    "a,b = get_value_range(mask_2D)\n",
    "tmp = [np.mean(a),np.mean(b)]\n",
    "tmp2 = np.max([a[1]-a[0],b[1]-b[0]])\n",
    "_r = [tmp[0]-tmp2/2,tmp[0]+tmp2/2,tmp[1]+tmp2/2,tmp[1]-tmp2/2]\n",
    "color_transform = {'PFC':0,'RSP':3}\n",
    "m = 1-get_contour(mask_2D>0)\n",
    "T = {}\n",
    "for i,j in enumerate(target):\n",
    "    if j in ['DTN','SG','PDTg','LDT','SLD']:\n",
    "        m = np.where(get_contour(np.sum(mask==i+1,axis=0)>0),0,m)\n",
    "        tmp = np.sum(mask==i+1,axis=0)\n",
    "        tmp[...,:570] *= 0\n",
    "        a,b = get_value_range(tmp)\n",
    "        T[j] = [np.mean(a),np.mean(b)]\n",
    "fig,ax = plt.subplots(figsize=(5,5))\n",
    "ax.matshow(m,cmap='gray')\n",
    "tmp = np.array(X['PFC']['terminal'])\n",
    "ax.scatter(tmp[:,2]/10,tmp[:,1]/10,color=color_pool[0],s=0.4)\n",
    "tmp = np.array(X['RSP']['terminal'])\n",
    "ax.scatter(tmp[:,2]/10,tmp[:,1]/10,color=color_pool[3],s=0.4)\n",
    "for i in T:\n",
    "    ax.text(T[i][0]-10,T[i][1],i,fontsize=fontsize_ticks)\n",
    "ax.set_xlim(_r[0],_r[1])\n",
    "ax.set_ylim(_r[2],_r[3])\n",
    "ax.set_title(\"Terminal in pontine central gray\",fontsize=fontsize_title)\n",
    "ax.set_axis_off()\n",
    "ax.legend(handles=[mpatches.Patch(color=color_pool[color_transform[i]], label=i) for i in color_transform],loc='lower right')\n",
    "plt.savefig(pons_central_path+'Terminal_in_central_gray.pdf',format='pdf',bbox_inches='tight',dpi=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('total terminals (PFC,RSP): ',len(X['PFC']['terminal']),',',len(X['RSP']['terminal']))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### medial and dorsal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "index_list = {'PRN':[brainstem_region.index(i) for i in ['PRNr','PRNc']],'SG':[brainstem_region.index(i) for i in ['NI','SG']],'NLL':[brainstem_region.index(i) for i in ['NLL']],'PB':[brainstem_region.index(i) for i in ['PB']],'LDT':[brainstem_region.index(i) for i in ['LDT']]}\n",
    "info = {}\n",
    "for i in neuron_to_brainstem_information:\n",
    "    d = neuron_to_brainstem_information[i]['soma_depth']\n",
    "    p = neuron_to_brainstem_information[i]['soma_flatten_position']\n",
    "    info[i] = [d,p]\n",
    "    for j in index_list:\n",
    "        tmp = 0.0\n",
    "        for k in index_list[j]:\n",
    "            tmp += neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][k]\n",
    "        info[i].append(tmp)\n",
    "result,soma_list = get_pdf(_flatmap[...,0],neuron_to_brainstem_information,info)\n",
    "color_transform = {'PRN':0,'SG':3,'NLL':2,'PB':1,'LDT':7}\n",
    "a = _flatmap*0.0\n",
    "b = []\n",
    "for i,j in zip(index_list,result):\n",
    "    c = np.expand_dims(j/np.max(j),axis=-1)\n",
    "    b.append([c,c*color_pool[color_transform[i]]])\n",
    "c = np.sum([i[0] for i in b],axis=0)\n",
    "c = np.where(c==0.0,1.0,c)\n",
    "merged_result = 0.0\n",
    "for i in b:\n",
    "    merged_result += i[0]/c*i[1]\n",
    "d = np.max([i[0] for i in b],axis=0)\n",
    "tmp = merged_result+(1-d)*np.array([1.0,1.0,1.0])\n",
    "for i in soma_list:\n",
    "    for k,l in zip(i[0],i[1]):\n",
    "        cv.circle(tmp,[int(np.round(k[1])), int(np.round(k[0]))],int(np.round(l*10))+1,np.array([0.5,0.5,0.5]),-1)\n",
    "fig = plt.figure(figsize=(20,20))\n",
    "plt.imshow(tmp,zorder=0)\n",
    "plt.imshow(flatmap_mask,zorder=1)\n",
    "plt.imshow(flatmap,zorder=2)\n",
    "plt.xlim(300,2700)\n",
    "plt.ylim(2700,300)\n",
    "plt.title('Dorsal tegmental',color='k')\n",
    "plt.axis('off')\n",
    "plt.axis('off')\n",
    "plt.axis(\"equal\")\n",
    "plt.savefig(pons_medial_path+'region_projection_to_dorsal_part.pdf',format='pdf')\n",
    "plt.savefig(pons_medial_path+'region_projection_to_dorsal_part.png',format='png')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "for i,j in zip(['220190017.swc','230065053.swc','221608013.swc','221645180.swc','220196167.swc','221509242.swc'],[0,3,2,2,1,5]):\n",
    "    neuronvis.clear()\n",
    "    neuronvis.addNeuronByID(i[:-7],i[-7:],color_pool[j],mirrorToRight=True,somaHide=False,dendriteHide=True,isLine=False)\n",
    "    set_view(neuronvis,'left')\n",
    "    neuronvis.render.savepng(pons_medial_path+i[:-4]+'.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "region_list = ['PRNr','PRNc','NI','SG']\n",
    "Y = []\n",
    "Z = []\n",
    "for i in neuron_to_brainstem_information:\n",
    "    Y.append(neuron_to_brainstem_information[i]['soma_flatten_position'])\n",
    "    Z.append(i)\n",
    "X = get_neuron_axon_info(Z,terminal_or_length='terminal',if_combined=True,if_log=False,filter=region_list)[1]\n",
    "Y = np.array(Y)\n",
    "weights = np.array([np.sum(i)/neuron_to_brainstem_information[j]['relative_density'] for i,j in zip(X,Z)])\n",
    "plt.imshow(_flatmap)\n",
    "custom_cmap = LinearSegmentedColormap.from_list('custom',[[1,1,1],cortical_module_color['VIS']*0.8])\n",
    "a = seaborn.kdeplot(Y[:,0]*2,Y[:,1]*2,weights=weights,fill=True,cmap=custom_cmap,common_norm=True,cumulative=False)\n",
    "a.imshow(flatmap_mask,zorder=1)\n",
    "a.imshow(flatmap,zorder=2)\n",
    "plt.xlim(300,2700)\n",
    "plt.ylim(2700,300)\n",
    "plt.title('Dorsal tegmental',color='k')\n",
    "plt.axis('off')\n",
    "plt.axis('off')\n",
    "plt.axis(\"equal\")\n",
    "plt.savefig(pons_medial_path+'region_projection_to_medial_part.pdf',format='pdf')\n",
    "plt.savefig(pons_medial_path+'region_projection_to_medial_part.png',format='png')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### tegmental pattern"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "BS = ['MB','P','MY']\n",
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "for i in BS:\n",
    "    neuronvis.addRegion(i)\n",
    "for i in ['PCG','NI','PDTg','DTN','LDT','LC']:\n",
    "    neuronvis.addRegion(i,[1.0,0.0,0.0])\n",
    "set_view(neuronvis,'dorsal')\n",
    "neuronvis.render.savepng(pons_medial_path+'tegmental_location.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "for i,j in zip(['233667190.swc'],[3]):\n",
    "    neuronvis.clear()\n",
    "    neuronvis.addNeuronByID(i[:-7],i[-7:],color_pool[j],mirrorToRight=True,somaHide=False,dendriteHide=True,isLine=False)\n",
    "    set_view(neuronvis,'left')\n",
    "    neuronvis.render.savepng(pons_medial_path+i[:-4]+'.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### medial"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pons_medial_path = pons_path+'medial/'\n",
    "create_dir(pons_medial_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "region_list = ['PRNr','PRNc']\n",
    "Y = []\n",
    "Z = []\n",
    "for i in neuron_to_brainstem_information:\n",
    "    Y.append(neuron_to_brainstem_information[i]['soma_flatten_position'])\n",
    "    Z.append(i)\n",
    "X = get_neuron_axon_info(Z,terminal_or_length='terminal',if_combined=True,if_log=False,filter=region_list)[1]\n",
    "Y = np.array(Y)\n",
    "weights = np.array([np.sum(i)/neuron_to_brainstem_information[j]['relative_density'] for i,j in zip(X,Z)])\n",
    "plt.imshow(_flatmap)\n",
    "custom_cmap = LinearSegmentedColormap.from_list('custom',[[1,1,1],cortical_module_color['Limbic']*0.8])\n",
    "a = seaborn.kdeplot(Y[:,0]*2,Y[:,1]*2,weights=weights,fill=True,cmap='',common_norm=True,cumulative=False)\n",
    "a.imshow(flatmap_mask,zorder=1)\n",
    "a.imshow(flatmap,zorder=2)\n",
    "plt.xlim(300,2700)\n",
    "plt.ylim(2700,300)\n",
    "plt.title('Pontine reticular formation',color='k')\n",
    "plt.axis('off')\n",
    "plt.axis('off')\n",
    "plt.axis(\"equal\")\n",
    "plt.savefig(pons_medial_path+'region_projection_to_PRF.pdf',format='pdf')\n",
    "plt.savefig(pons_medial_path+'region_projection_to_PRF.png',format='png')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "region_list = ['NI','SG']\n",
    "Y = []\n",
    "Z = []\n",
    "for i in neuron_to_brainstem_information:\n",
    "    Y.append(neuron_to_brainstem_information[i]['soma_flatten_position'])\n",
    "    Z.append(i)\n",
    "X = get_neuron_axon_info(Z,terminal_or_length='terminal',if_combined=True,if_log=False,filter=region_list)[1]\n",
    "Y = np.array(Y)\n",
    "weights = np.array([np.sum(i)/neuron_to_brainstem_information[j]['relative_density'] for i,j in zip(X,Z)])\n",
    "plt.imshow(_flatmap)\n",
    "custom_cmap = LinearSegmentedColormap.from_list('custom',[[1,1,1],cortical_module_color['VIS']*0.8])\n",
    "a = seaborn.kdeplot(Y[:,0]*2,Y[:,1]*2,weights=weights,fill=True,cmap=custom_cmap,common_norm=True,cumulative=False)\n",
    "a.imshow(flatmap_mask,zorder=1)\n",
    "a.imshow(flatmap,zorder=2)\n",
    "plt.xlim(300,2700)\n",
    "plt.ylim(2700,300)\n",
    "plt.title('Dorsal tegmental',color='k')\n",
    "plt.axis('off')\n",
    "plt.axis('off')\n",
    "plt.axis(\"equal\")\n",
    "plt.savefig(pons_medial_path+'region_projection_to_DT.pdf',format='pdf')\n",
    "plt.savefig(pons_medial_path+'region_projection_to_DT.png',format='png')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "index_list = {'PRN':[brainstem_region.index(i) for i in ['PRNr','PRNc']],'SG':[brainstem_region.index(i) for i in ['NI','SG']],'NLL':[brainstem_region.index(i) for i in ['NLL']],'PB':[brainstem_region.index(i) for i in ['PB']],'LDT':[brainstem_region.index(i) for i in ['LDT']]}\n",
    "info = {}\n",
    "for i in neuron_to_brainstem_information:\n",
    "    d = neuron_to_brainstem_information[i]['soma_depth']\n",
    "    p = neuron_to_brainstem_information[i]['soma_flatten_position']\n",
    "    info[i] = [d,p]\n",
    "    for j in index_list:\n",
    "        tmp = 0.0\n",
    "        for k in index_list[j]:\n",
    "            tmp += neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][k]\n",
    "        info[i].append(tmp)\n",
    "r = 200\n",
    "s = 125\n",
    "transition_color = np.array([[255,255,255],[161,238,204],[255,242,193],[246,49,141]])/255.0\n",
    "fig,axes = plt.subplots(2,3,figsize=(len(index_list)/2*5,8))\n",
    "kernel = generate_gaussian_kernel(r,s)\n",
    "for i,j in enumerate(index_list):\n",
    "    c = 0.0\n",
    "    tmp = _flatmap[...,0]*0.0\n",
    "    soma_list = [[],[]]\n",
    "    for k in info:\n",
    "        d,p = info[k][:2]\n",
    "        e = info[k][2:]\n",
    "        fill_kernel(tmp,np.array(p[::-1])*2,e[i]/neuron_to_brainstem_information[k]['relative_density'],r,kernel)\n",
    "        soma_list[0].append(np.array(p[::-1])*2)\n",
    "        soma_list[1].append(e[i])\n",
    "    soma_list[1] = np.array(soma_list[1])**0.5\n",
    "    soma_list[1] /= np.max(soma_list[1])\n",
    "    result = plot_transition_color(transition_color,tmp)\n",
    "    result = np.where(_flatmap==0,0,result)\n",
    "    for k,l in zip(soma_list[0],soma_list[1]):\n",
    "        cv.circle(result,[int(np.round(k[1])), int(np.round(k[0]))],int(np.round(l*10))+1,np.array([0.5,0.5,0.5]),-1)\n",
    "    result = np.where(flatmap_mask[...,:3]==0,result,1.0)\n",
    "    axes[i//3,i%3].imshow(result[400:-400,400:-400])\n",
    "    axes[i//3,i%3].set_axis_off()\n",
    "    axes[i//3,i%3].set_title(j)\n",
    "plt.savefig(pons_medial_path+'terminal_origion.pdf',format='pdf')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mask = 0.0\n",
    "for i in ['PCG','LDT','PDTg','DTN','NI','SG','LC','SLC']:\n",
    "    mask += get_region_mask(i)\n",
    "X = {}\n",
    "for id,neuron in enumerate(neuron_to_brainstem_information):\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']\n",
    "    if region not in ['RSP','VIS']:\n",
    "        continue\n",
    "    module = region\n",
    "    if module not in X:\n",
    "        X[module] = {'terminal':[],'color':[]}\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            X[module]['terminal'].append(terminal.copy())\n",
    "            if neuron_terminals[neuron]['mirror_mark']:\n",
    "                X[module]['terminal'][-1][2] = 11400-X[module]['terminal'][-1][2]\n",
    "background_cronal = (np.sum(mask,axis=0)>0).astype(float)\n",
    "background_sagittal = (np.sum(mask,axis=-1)>0).astype(float).T\n",
    "background_dorsal = (np.sum(mask,axis=1)>0).astype(float)\n",
    "for i in X:\n",
    "    if len(X[i]['terminal'])==0:\n",
    "        continue\n",
    "    X_cronal = np.array(X[i]['terminal'])[...,1:]/10\n",
    "    X_sagittal = np.array(X[i]['terminal'])[...,:2]/10\n",
    "    X_dorsal = np.concatenate([np.array(X[i]['terminal'])[:,:1],np.array(X[i]['terminal'])[:,2:]],axis=-1)/10\n",
    "    a = X_sagittal[:,0].copy()\n",
    "    X_sagittal[:,0] = X_sagittal[:,1]*1\n",
    "    X_sagittal[:,1] = a\n",
    "    custom_cmap = LinearSegmentedColormap.from_list('custom',[[1,1,1],color_pool[0 if i=='RSP' else 3]])\n",
    "    plot_points_distribution_in_mask(X_cronal,background_cronal,title=i+'_cronal',save_path=pons_medial_path+'distribution_'+i+'_cronal',cmap=custom_cmap)\n",
    "    plot_points_distribution_in_mask(X_sagittal,background_sagittal,title=i+'_sagittal',save_path=pons_medial_path+'distribution_'+i+'_sagittal',cmap=custom_cmap)\n",
    "    plot_points_distribution_in_mask(X_dorsal,background_dorsal,title=i+'_dorsal',save_path=pons_medial_path+'distribution_'+i+'_dorsal',cmap=custom_cmap)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### dorsal lateral"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pons_dorsal_lateral_path = pons_path+'dorsal_lateral/'\n",
    "create_dir(pons_dorsal_lateral_path)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### ipsi and contral difference"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "region_list = ['NLL','PB','LDT']\n",
    "X = {}\n",
    "for neuron in neuron_to_brainstem_information:\n",
    "    region = cortical_module_transform[neuron_to_brainstem_information[neuron]['cortex_region']]\n",
    "    if region not in X:\n",
    "        X[region] = []\n",
    "    X[region].append(neuron)\n",
    "for i in X:\n",
    "    X[i] = get_neuron_axon_info(X[i],terminal_or_length='terminal',if_combined=False,if_log=False,filter=region_list)[1]\n",
    "tmp = np.array([np.sum(tmp[0],axis=0),np.sum(tmp[1],axis=0),np.sum(tmp[2],axis=0)])\n",
    "tmp = np.concatenate([tmp[:,:3],tmp[:,-1:-4:-1]],axis=-1)\n",
    "x_labels = ['AUD','Insula','PFC']\n",
    "a = []\n",
    "b = [i+'_ipsi' for i in region_list]+[i+'_contra' for i in region_list[::-1]]\n",
    "c = []\n",
    "for i,j in zip(x_labels,tmp):\n",
    "    a.append(i+'(n='+str(np.sum(j))+')')\n",
    "    c.append(j/np.max(j))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.matshow(c,cmap='BuGn')\n",
    "plt.xticks(range(len(b)),b,fontsize=fontsize_ticks)\n",
    "plt.yticks(range(len(a)),a,fontsize=fontsize_ticks)\n",
    "plt.xlabel('Target',fontsize=fontsize_label)\n",
    "plt.ylabel('Source',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.title('Relative strength',fontsize=fontsize_title)\n",
    "plt.savefig(pons_dorsal_lateral_path+'bothside_preferences.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### TEa-AI-PFC bands"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "amygdala_region = ['COA','LA','BLA','BMA','PA','AAA','CEA','MEA']\n",
    "flatmap = _flatmap*1\n",
    "threshold = 10\n",
    "#a = get_neuron_axon_info(cortex_neuron_name,terminal_or_length='terminal',if_combined=True,if_log=False,filter=amygdala_region)[1]\n",
    "for i,j in zip(cortex_neuron_name,a):\n",
    "    k = [j[0],np.sum(j[1:-3]),np.sum(j[-3:])]\n",
    "    if np.sum(k)<threshold:\n",
    "        continue\n",
    "    k = np.array(k)/np.sum(k)\n",
    "    l = neuron_to_brainstem_information[i]['soma_flatten_position']\n",
    "    cv.circle(flatmap,[int(l[0])*2, int(l[1])*2],2*int(np.sum(j)**0.5),np.array(k)*255,-1)\n",
    "cv.circle(flatmap,[320,320],10,np.array([0.0,0.0,0.0])*255,-1)\n",
    "cv.circle(flatmap,[320,420],20,np.array([0.0,0.0,0.0])*255,-1)\n",
    "cv.circle(flatmap,[320,520],30,np.array([0.0,0.0,0.0])*255,-1)\n",
    "fig = plt.figure(figsize=(10,10))\n",
    "plt.imshow(flatmap)\n",
    "plt.xlim(300,2700)\n",
    "plt.ylim(2700,300)\n",
    "plt.axis(\"equal\")\n",
    "plt.axis('off')\n",
    "plt.title('Amygdala projection',fontsize=fontsize_title)\n",
    "plt.savefig(pons_dorsal_lateral_path+'amygdala_projection.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### statistics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = {}\n",
    "for id,neuron in enumerate(neuron_to_brainstem_information):\n",
    "    region = cortical_module_transform[neuron_to_brainstem_information[neuron]['cortex_region']]\n",
    "    if region not in X:\n",
    "        X[region] = []\n",
    "    X[region].append(neuron)\n",
    "for region in X:\n",
    "    X[region] = get_neuron_axon_info(X[region],terminal_or_length='terminal',if_combined=True,if_log=False,filter=brainstem_region_hierachy['P'])[1]\n",
    "for region in X:\n",
    "    tmp = np.sum(X[region],axis=0)\n",
    "    a = tmp[brainstem_region_hierachy['P'].index('PG')]\n",
    "    b = tmp[brainstem_region_hierachy['P'].index('TRN')]\n",
    "    c = np.sum(tmp)\n",
    "    print(region,a,b,c,np.round((a+b)/c*100,0))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Midbrain"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "midbrain_path = brainstem_path+'midbrain/'\n",
    "create_dir(midbrain_path)\n",
    "MB_mask = get_region_mask('MB')\n",
    "r = 25"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### axon"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if not os.path.exists(midbrain_path+'projection_axon_info.npy'):\n",
    "    mask = get_region_mask('MB')\n",
    "    import random\n",
    "    _max = 200\n",
    "    color_transform = {'VIS':color_pool[3],'ACA':color_pool[4],'MOs':color_pool[0],'SSs':color_pool[1],'AUD':color_pool[2],'AI':color_pool[5],'SSp':color_pool[1],'MOp':color_pool[3],'TEa':color_pool[5],'PL':color_pool[7],'ORB':color_pool[6],'ILA':color_pool[8],'RSP':color_pool[9],}\n",
    "    info = {}\n",
    "    for neuron in tool_bar(cortex_neuron_name,desc='iteration on neurons (generating mapping colors) ...',leave=False,colour='#55AF7B'):\n",
    "        region = neuron_to_brainstem_information[neuron]['cortex_region']\n",
    "        if region not in info:\n",
    "            info[region] = {'neuron':[]}\n",
    "        info[region]['neuron'].append(neuron)\n",
    "    for region in info:\n",
    "        if region not in color_transform:\n",
    "            continue\n",
    "        info[region]['axon'] = get_terminal_lines_in_mask(info[region]['neuron'] if len(info[region]['neuron'])<=_max else random.sample(info[region]['neuron'],_max),mask,color=color_transform[region])\n",
    "    np.save(midbrain_path+'projection_axon_info.npy',info)\n",
    "else:\n",
    "    info = np.load(midbrain_path+'projection_axon_info.npy',allow_pickle=True).item()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### sensory input"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if not os.path.exists(midbrain_path+'projection_sensory_axon_info.npy'):\n",
    "    mask = get_region_mask('MB')\n",
    "    import random\n",
    "    _max = 200\n",
    "    color_transform = {'VISp':color_pool[3],'AUDp':color_pool[2],'SSp':color_pool[1]}\n",
    "    info = {}\n",
    "    for neuron in tool_bar(cortex_neuron_name,desc='iteration on neurons (generating mapping colors) ...',leave=False,colour='#55AF7B'):\n",
    "        region = neuron_to_brainstem_information[neuron]['cortex_region']\n",
    "        if region in ['VIS','AUD']:\n",
    "            region += neuron_to_brainstem_information[neuron]['cortex_subregion']\n",
    "        if region not in info:\n",
    "            info[region] = {'neuron':[]}\n",
    "        info[region]['neuron'].append(neuron)\n",
    "    for region in info:\n",
    "        if region not in color_transform:\n",
    "            continue\n",
    "        info[region]['axon'] = get_terminal_lines_in_mask(info[region]['neuron'] if len(info[region]['neuron'])<=_max else random.sample(info[region]['neuron'],_max),mask,color=color_transform[region])\n",
    "    np.save(midbrain_path+'projection_axon_info.npy',info)\n",
    "else:\n",
    "    info = np.load(midbrain_path+'projection_sensory_axon_info.npy',allow_pickle=True).item()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear()\n",
    "if 1:\n",
    "    f,d = draw_lines,0#draw_lines,0#draw_points,2\n",
    "    for i,j in zip(['AUD','SSp','VIS'],[2,1,3]):\n",
    "        tmp = []\n",
    "        for k in info[i]['axon'][d]:\n",
    "            tmp.append([])\n",
    "            for l in k:\n",
    "                tmp[-1].append(l.copy())\n",
    "                tmp[-1][-1][-1] = 11400-tmp[-1][-1][-1]\n",
    "        f(neuronvis,tmp,set_color(info[i]['axon'][d],color_pool[j]))\n",
    "    neuronvis.render.setPointSize(1)\n",
    "    for i in ['MB']:\n",
    "        neuronvis.addRegion(i)\n",
    "    #set_view(neuronvis,'left')\n",
    "    neuronvis.render.savepng(midbrain_path+'sensory_system_axons.png')\n",
    "else:\n",
    "    f,d = draw_points,2#draw_lines,0#draw_points,2\n",
    "    for i,j in zip(['AUD','SSp','VIS'],[2,1,3]):\n",
    "        tmp = []\n",
    "        for k in info[i]['axon'][d]:\n",
    "            tmp.append(k.copy())\n",
    "            tmp[-1][-1] = 11400-tmp[-1][-1]\n",
    "        f(neuronvis,tmp,set_color(info[i]['axon'][d],color_pool[j]))\n",
    "    neuronvis.render.setPointSize(1)\n",
    "    for i in ['MB']:\n",
    "        neuronvis.addRegion(i)\n",
    "    #set_view(neuronvis,'left')\n",
    "    neuronvis.render.savepng(midbrain_path+'sensory_system_points.png')\n",
    "neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### dorsal vs ventral"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "info = {'VISp':[],'SSp':[],'AUDp':[]}\n",
    "source = ['VISp','SSp','AUDp']\n",
    "target = ['MBsen','MBmot','MBsta','PRT']\n",
    "for neuron in tool_bar(cortex_neuron_name,desc='iteration on neurons (generating mapping colors) ...',leave=False,colour='#55AF7B'):\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']\n",
    "    if region in ['AUD','VIS']:\n",
    "        region += neuron_to_brainstem_information[neuron]['cortex_subregion']\n",
    "    if region not in info:\n",
    "        continue\n",
    "    info[region].append(get_axonal_statics_by_regions(neuron,target,'terminalregion'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "info2 = {'VISp':[],'SSp':[],'AUDp':[]}\n",
    "target2 = ['APN','SCs','SCm','IC']\n",
    "for neuron in tool_bar(cortex_neuron_name,desc='iteration on neurons (generating mapping colors) ...',leave=False,colour='#55AF7B'):\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']\n",
    "    if region in ['AUD','VIS']:\n",
    "        region += neuron_to_brainstem_information[neuron]['cortex_subregion']\n",
    "    if region not in info:\n",
    "        continue\n",
    "    info2[region].append(get_axonal_statics_by_regions(neuron,target2,'terminalregion'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "A = []\n",
    "for index,i,j in zip(range(len(source)),source,[3,1,2]):\n",
    "    tmp = np.sum(info[i],axis=0)\n",
    "    tmp2 = np.sum(info2[i],axis=0)\n",
    "    tmp[0] = tmp[0]+tmp[3]+tmp2[2]\n",
    "    tmp[1] = tmp[1]+tmp[2]-tmp[3]-tmp2[2]\n",
    "    A.append(tmp[:2])\n",
    "B = []\n",
    "for index,i,j in zip(range(len(source)),source,[3,1,2]):\n",
    "    tmp = np.sum(info2[i])\n",
    "    B.append(tmp)\n",
    "print(B,np.array(A)[:,0],B/np.array(A)[:,0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "width = 0.15\n",
    "plt.figure(figsize=(3,5))\n",
    "for index,i,j in zip(range(len(source)),source,[3,1,2]):\n",
    "    tmp = np.mean(info[i],axis=0)\n",
    "    tmp2 = np.mean(info2[i],axis=0)\n",
    "    tmp[0] = tmp[0]+tmp[3]+tmp2[2]\n",
    "    tmp[1] = tmp[1]+tmp[2]-tmp[3]-tmp2[2]\n",
    "    print(tmp[:2])\n",
    "    plt.bar([g+width*index*1.2 for g in range(2)],tmp[:2],width=width,label=i,color=color_pool[j])\n",
    "plt.xticks([i+width/2 for i in range(2)],['dorsal','ventral'],fontsize=fontsize_ticks)\n",
    "plt.xlabel('Target',fontsize=fontsize_label)\n",
    "plt.ylabel('Number',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.title('Dorsal ventral midbrain projection comparison',fontsize=fontsize_title)\n",
    "plt.savefig(midbrain_path+'sensory_in_alar_basal_statistics.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "width = 0.15\n",
    "plt.figure(figsize=(2,5))\n",
    "X = []\n",
    "for index,i,j in zip(range(len(source)),source,[3,1,2]):\n",
    "    tmp = np.mean(info2[i],axis=0)\n",
    "    X.append(tmp)\n",
    "X = np.array(X)\n",
    "X /= np.sum(X,axis=1).reshape(-1,1)\n",
    "print(X*100)\n",
    "c = 0\n",
    "c_t = {'APN':0,'SCs':3,'SCm':1,'IC':2}\n",
    "for i,j in enumerate(target2):\n",
    "    plt.bar([g*0.5 for g in range(len(source))],X[:,i]+c,width=width,label=j,color=color_pool[c_t[j]],zorder=-i)\n",
    "    c += X[:,i]\n",
    "plt.legend()\n",
    "plt.xticks([i*0.5 for i in range(len(source))],source,fontsize=fontsize_ticks)\n",
    "plt.xlabel('Source',fontsize=fontsize_label)\n",
    "plt.ylabel('Ratio',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.title('Dorsal midbrain detail',fontsize=fontsize_title)\n",
    "plt.savefig(midbrain_path+'sensory_in_alar_statistics.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### vis-ssp-aud differences"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "for i,j,k in zip(['VIS','SSp_bfd','AUD'],['221481548.swc','221073042.swc','221423050.swc'],[3,1,2]):\n",
    "    neuronvis.clear()\n",
    "    neuron_tree = iondata.getNeuronTreeByID(j[:-7],j[-7:])\n",
    "    neuron_tree.somaRadius = 90\n",
    "    neuronvis.addNeuronTree(neuron_tree,axonColor=color_pool[k],somaColor=[0.5,0.5,0.5],dendriteColor=[0.5,0.5,0.5],mirrorToRight=True,isLine=False)\n",
    "    for l in ['MB']:\n",
    "        neuronvis.addRegion(l)\n",
    "    #set_view(neuronvis,'right')\n",
    "    neuronvis.render.savepng(midbrain_path+'sensory_system_'+i+'.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i,j,k in zip(['VIS','SSp_bfd','AUD'],['221481548.swc','221073042.swc','221481674.swc'],[3,1,2]):\n",
    "    neuronvis = nv.neuronVis(size=(1200,900))\n",
    "    neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "    set_view(neuronvis,'anterior')\n",
    "    neuron_tree = iondata.getNeuronTreeByID(j[:-7],j[-7:])\n",
    "    neuron_tree.somaRadius = 90\n",
    "    neuronvis.addNeuronTree(neuron_tree,axonColor=color_pool[k],somaColor=[0.5,0.5,0.5],dendriteColor=[0.5,0.5,0.5],mirrorToRight=True,isLine=True)\n",
    "    for l in ['MB']:\n",
    "        neuronvis.addRegion(l)\n",
    "    neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### difference statistics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "info = {}\n",
    "threshold = 10\n",
    "mask = 0.0\n",
    "for i in ['SCs','SCm','IC']:\n",
    "    mask += get_region_mask(i)\n",
    "for i in neuron_to_brainstem_information:\n",
    "    region = neuron_to_brainstem_information[i]['cortex_region']\n",
    "    if region in ['VIS','AUD']:\n",
    "        region += neuron_to_brainstem_information[i]['cortex_subregion']\n",
    "    if region not in ['VISp','SSp','AUDp']:\n",
    "        continue\n",
    "    tmp = []\n",
    "    for terminal in neuron_terminals[i]['terminals']:\n",
    "            if if_inside_mask(mask,np.array(terminal)/10):\n",
    "                tmp.append(terminal.copy())\n",
    "    if len(tmp)<threshold:\n",
    "        continue\n",
    "    tmp = abs(np.linalg.svd(np.array(tmp).T,compute_uv=False))\n",
    "    if region not in info:\n",
    "        info[region] = [[],[]]\n",
    "    info[region][0].append(tmp[0]*tmp[1])\n",
    "    info[region][1].append(tmp[0]*tmp[1]*tmp[2])\n",
    "x_labels = ['VISp', 'SSp', 'AUDp']\n",
    "medianprops = dict(linewidth=1.5,color='#A81E32')\n",
    "plt.boxplot([np.log(np.array(info[i][0])+1) for i in x_labels],medianprops=medianprops)\n",
    "plt.xticks([i+1 for i in range(len(x_labels))],[i+'(n='+str(len(info[i][0]))+')' for i in x_labels],fontsize=fontsize_ticks)\n",
    "plt.xlabel('Modality',fontsize=fontsize_label)\n",
    "plt.ylabel('Area (log(\\u00B5m**2+1))',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.title('Axon Area comparison',fontsize=fontsize_title)\n",
    "plt.savefig(midbrain_path+'sensory_difference_area_statistics.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()\n",
    "plt.boxplot([np.log(np.array(info[i][1])+1) for i in x_labels],medianprops=medianprops)\n",
    "plt.xticks([i+1 for i in range(len(x_labels))],[i+'(n='+str(len(info[i][1]))+')' for i in x_labels],fontsize=fontsize_ticks)\n",
    "plt.xlabel('Modality',fontsize=fontsize_label)\n",
    "plt.ylabel('Volume (log(\\u00B5m**3+1))',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.title('Axon volume comparison',fontsize=fontsize_title)\n",
    "plt.savefig(midbrain_path+'sensory_difference_volume_statistics.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### VIS/SS/AUD comparison"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp = {'SS':['SSp','SSs'],'VIS':['VIS','PTLp'],'AUD':['AUD']}\n",
    "transform = {}\n",
    "for modality in tmp:\n",
    "    for region in tmp[modality]:\n",
    "        transform[region] = modality\n",
    "color = {'SS':color_pool[1].tolist(),'VIS':color_pool[3].tolist(),'AUD':color_pool[4].tolist()}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mask = 0\n",
    "for i in ['SCs','SCm','IC']:\n",
    "    mask += get_region_mask(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = {i:[[],[]] for i in tmp}\n",
    "for i in neuron_to_brainstem_information:\n",
    "    try:\n",
    "        modality =  transform[neuron_to_brainstem_information[i]['cortex_region']]\n",
    "    except:\n",
    "        continue\n",
    "    for terminal in neuron_terminals[i]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            result[modality][0].append(terminal.copy())\n",
    "            if neuron_terminals[i]['mirror_mark']:\n",
    "                result[modality][0][-1][2] = 11400-result[modality][0][-1][2]\n",
    "            result[modality][1].append(color[modality]) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear()\n",
    "for i in result:\n",
    "    if i=='VIS':\n",
    "        continue\n",
    "    draw_points(neuronvis,result[i][0],result[i][1])\n",
    "set_view(neuronvis,'dorsal')\n",
    "neuronvis.render.savepng(midbrain_path+'VIS_SS_AUD.png')\n",
    "neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear()\n",
    "for i in result:\n",
    "    draw_points(neuronvis,result[i][0],result[i][1])\n",
    "neuronvis.render.savepng(midbrain_path+'VIS_SS_AUD.png')\n",
    "neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear()\n",
    "for i in result:\n",
    "    if i=='VIS':\n",
    "        continue\n",
    "    draw_points(neuronvis,result[i][0],result[i][1])\n",
    "neuronvis.addRegion('PAG')\n",
    "neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### SSp-bfd project to SCm dominantly"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = {}\n",
    "c = 0\n",
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear()\n",
    "for i in neuron_to_brainstem_information:\n",
    "    if neuron_to_brainstem_information[i]['cortex_region'] == 'SSp':\n",
    "        region = neuron_to_brainstem_information[i]['cortex_subregion']\n",
    "        if region not in result:\n",
    "            result[region] = color_pool[c]\n",
    "            c += 1\n",
    "        neuronvis.addNeuronByID(i[:-7],i[-7:],result[region],mirrorToRight=True,somaHide=False,dendriteHide=True,isLine=True)\n",
    "set_view(neuronvis,'left')\n",
    "neuronvis.render.savepng(midbrain_path+'SSp_subregions_SCm_projection.png')\n",
    "neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = {}\n",
    "c = 0\n",
    "X = []\n",
    "Y = []\n",
    "mask = get_region_mask('SCm')\n",
    "for i in neuron_to_brainstem_information:\n",
    "    region = neuron_to_brainstem_information[i]['cortex_region']\n",
    "    if region not in ['SSp']:\n",
    "        continue\n",
    "    region = neuron_to_brainstem_information[i]['cortex_subregion'][1:]\n",
    "    if region not in result:\n",
    "            result[region] = color_pool[c].tolist()\n",
    "            c += 1\n",
    "    for terminal in neuron_terminals[i]['terminals']:\n",
    "            if if_inside_mask(mask,np.array(terminal)/10):\n",
    "                X.append(terminal.copy())\n",
    "                Y.append(result[region])\n",
    "                if neuron_terminals[i]['mirror_mark']:\n",
    "                    X[-1][2] = 11400-X[-1][2]\n",
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "draw_points(neuronvis,X,Y)\n",
    "neuronvis.render.setPointSize(2)\n",
    "neuronvis.addRegion('SCm')\n",
    "for i in ['dorsal','anterior','posterior']:\n",
    "    set_view(neuronvis,i)\n",
    "    neuronvis.render.savepng(midbrain_path+'SSp_subregions_SCm_projection_terminal_'+i+'.png')\n",
    "neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "draw_points(neuronvis,X,Y)\n",
    "neuronvis.render.setPointSize(2)\n",
    "neuronvis.addRegion('SCs',[1,0,0])\n",
    "neuronvis.addRegion('SCdg',[1,0,0])\n",
    "for i in ['dorsal','anterior','posterior']:\n",
    "    set_view(neuronvis,i)\n",
    "    neuronvis.render.savepng(midbrain_path+'SSp_subregions_SCsm_projection_terminal_'+i+'.png')\n",
    "neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "neuronvis.addRegion('SCs',[0,0,1])\n",
    "neuronvis.addRegion('SCm',[1,0,0])\n",
    "set_view(neuronvis,'dorsal')\n",
    "neuronvis.render.savepng(midbrain_path+'SCs_SCm_location.png')\n",
    "neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = {}\n",
    "for i in neuron_to_brainstem_information:\n",
    "    if neuron_to_brainstem_information[i]['cortex_region'] == 'SSp':\n",
    "        region = neuron_to_brainstem_information[i]['cortex_subregion']\n",
    "        if region not in result:\n",
    "            result[region] = []\n",
    "        result[region].append([neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][brainstem_region.index('APN')],neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][brainstem_region.index('SCm')],np.sum(neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][brainstem_region.index('APN'):])])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_label = ['-bfd','-ul','-ll','-tr','-n','-m']\n",
    "for i,j in enumerate(x_label):\n",
    "    tmp = np.sum(np.array(result[j]),axis=0)\n",
    "    print(j,tmp/np.max(tmp))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_label = ['-bfd','-ul','-ll','-tr','-n','-m']#['-bfd','-ll','-tr','-ul','-n','-m']\n",
    "a = {}\n",
    "for i,j in enumerate(x_label):\n",
    "    tmp = np.array(result[j])\n",
    "    a[j] = plt.hist(tmp[:,0],label=j,histtype='step',cumulative=True,density=True,range=(0,500),bins=200)\n",
    "    #plt.scatter(tmp[:,0]*0+i+0.2,tmp[:,1],label=j)\n",
    "#plt.xticks(range(len(x_label)),x_label)\n",
    "plt.legend()\n",
    "plt.show()\n",
    "from scipy.ndimage import gaussian_filter1d\n",
    "x_label = ['-bfd','-tr','-n','-ul','-m','-ll']\n",
    "for i,k in zip(x_label,range(len(x_label))):\n",
    "    r = k/(len(x_label)-1)\n",
    "    j = a[i]\n",
    "    plt.plot(0.5*(j[1][1:]+j[1][:-1]),gaussian_filter1d(j[0],55),label=i,color=r*color_pool[3]+(1-r)*color_pool[0])\n",
    "plt.xticks([0,250,500],[0,250,500],fontsize=fontsize_ticks)\n",
    "plt.yticks([0.9,0.95,1],[0.9,0.95,1],fontsize=fontsize_ticks)\n",
    "plt.xlabel('Terminal number',fontsize=fontsize_label)\n",
    "plt.ylabel('Probability',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.title('SSp subarea in APN (cumulative curve)',fontsize=fontsize_title)\n",
    "plt.legend()\n",
    "plt.savefig(midbrain_path+'SSp_in_APN.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_label = ['-bfd','-ul','-ll','-tr','-n','-m']#['-bfd','-ll','-tr','-ul','-n','-m']\n",
    "a = {}\n",
    "for i,j in enumerate(x_label):\n",
    "    tmp = np.array(result[j])\n",
    "    a[j] = plt.hist(tmp[:,1],label=j,histtype='step',cumulative=True,density=True,range=(0,500),bins=200)\n",
    "    #plt.scatter(tmp[:,0]*0+i+0.2,tmp[:,1],label=j)\n",
    "#plt.xticks(range(len(x_label)),x_label)\n",
    "plt.legend()\n",
    "plt.show()\n",
    "from scipy.ndimage import gaussian_filter1d\n",
    "x_label = ['-bfd','-tr','-n','-ul','-m','-ll']\n",
    "for i,k in zip(x_label,range(len(x_label))):\n",
    "    r = k/(len(x_label)-1)\n",
    "    j = a[i]\n",
    "    plt.plot(0.5*(j[1][1:]+j[1][:-1]),gaussian_filter1d(j[0],55),label=i,color=r*color_pool[3]+(1-r)*color_pool[0])\n",
    "plt.xticks([0,250,500],[0,250,500],fontsize=fontsize_ticks)\n",
    "plt.yticks([0.7,0.85,1],[0.7,0.85,1],fontsize=fontsize_ticks)\n",
    "plt.xlabel('Terminal number',fontsize=fontsize_label)\n",
    "plt.ylabel('Probability',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.title('SSp subarea in SCm (cumulative curve)',fontsize=fontsize_title)\n",
    "plt.legend()\n",
    "plt.savefig(midbrain_path+'SSp_in_SCm.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### SSp-bfd under SCs coverage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "SCs_mask = get_region_mask('SCs')\n",
    "SCdg_mask = get_region_mask('SCdg')\n",
    "mask = get_region_mask('SCm')\n",
    "info = {}\n",
    "tmp = {}\n",
    "position_mask = np.array([i for i in range(SCs_mask.shape[1])])\n",
    "for i in neuron_to_brainstem_information:\n",
    "    region = neuron_to_brainstem_information[i]['cortex_region']\n",
    "    if region not in ['SSp']:\n",
    "        continue\n",
    "    region = neuron_to_brainstem_information[i]['cortex_subregion'][1:]\n",
    "    if region not in info:\n",
    "        info[region] = [0.0,0.0]\n",
    "    for terminal in neuron_terminals[i]['terminals']:\n",
    "            if if_inside_mask(mask,np.array(terminal)/10):\n",
    "                s = int(terminal[0]/10)\n",
    "                if s not in tmp:\n",
    "                    a,b = get_value_range(SCs_mask[s])\n",
    "                    c = np.mean(position_mask,where=SCs_mask[s][:,a[0]].astype(bool))\n",
    "                    if np.isnan(s):\n",
    "                        continue\n",
    "                    p1 = np.array([a[0],len(position_mask)-c])\n",
    "                    a,b = get_value_range(SCdg_mask[s])\n",
    "                    c = np.mean(position_mask,where=SCdg_mask[s][:,a[0]].astype(bool))\n",
    "                    if np.isnan(s):\n",
    "                        continue\n",
    "                    p2 = np.array([a[0],len(position_mask)-c])\n",
    "                    tmp[s] = [p1,p2]\n",
    "                p1,p2 = tmp[s]\n",
    "                p0 = np.array([terminal[2]/10,1000-terminal[1]/10])\n",
    "                if p0[0]>570:\n",
    "                    p0[0] = 1140-p0[0]\n",
    "                a,b = p0-p1,p0-p2\n",
    "                if (a[0]*b[1]+a[1]*b[0])>0:\n",
    "                    info[region][0] += 1\n",
    "                info[region][1] += 1\n",
    "tmp = [[i,info[i][0]/info[i][1]] for i in info]\n",
    "tmp = sorted(tmp,key=lambda x:x[1],reverse=True)\n",
    "source = []\n",
    "result = []\n",
    "for i in tmp:\n",
    "    source.append(i[0])\n",
    "    result.append(i[1])\n",
    "plt.bar(range(len(result)),result,width=0.25,color='#A81E32')\n",
    "plt.xticks(range(len(source)),[i+' (n='+str(int(info[i][1]))+')' for i in source],fontsize=fontsize_ticks,rotation=-50)\n",
    "plt.xlabel('Source',fontsize=fontsize_label)\n",
    "plt.ylabel('Ratio',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.title('Terminal under SCs',fontsize=fontsize_title)\n",
    "plt.savefig(midbrain_path+'SSp_subregion_and_SCs_statistics.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "info"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### SSp-bfd has two branches"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "for neuron in Scene.scene2List(midbrain_path+'SSp-bfd.nv'):\n",
    "    neuron_tree = iondata.getNeuronTreeByID(neuron['sampleid'],neuron['name'])\n",
    "    neuron_tree.somaRadius = 10\n",
    "    neuronvis.addNeuronTree(neuron_tree,axonColor=np.array([neuron['color']['r'],neuron['color']['g'],neuron['color']['b']]).astype(float)/255.0,somaColor=[0.5,0.5,0.5],dendriteColor=[0.5,0.5,0.5],dendriteHide=True,mirrorToRight=True,isLine=True)\n",
    "for l in ['SCm']:\n",
    "    neuronvis.addRegion(l)\n",
    "neuronvis.render.setLookAt(eye=(-10000,-10000,-10000),center=(0,0,0),up=(0,-1,0))\n",
    "neuronvis.render.savepng(midbrain_path+'SSp_bfd_2_branches_default.png')\n",
    "for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "    set_view(neuronvis,i)\n",
    "    neuronvis.render.savepng(midbrain_path+'SSp_bfd_2_branches_'+i+'.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mask = get_region_mask('SCm')\n",
    "X = {}\n",
    "for id,neuron in enumerate(neuron_to_brainstem_information):\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']+neuron_to_brainstem_information[neuron]['cortex_subregion']\n",
    "    if region not in X:\n",
    "        X[region] = {'terminal':[]}\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            X[region]['terminal'].append(terminal.copy())\n",
    "            if neuron_terminals[neuron]['mirror_mark']:\n",
    "                X[region]['terminal'][-1][2] = 11400-X[region]['terminal'][-1][2]\n",
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "for ii,i in enumerate(X):\n",
    "    neuronvis.addRegion('SCm')\n",
    "    draw_points(neuronvis,X[i]['terminal'],[[1,0,0] if ii==0 else [0,0,1] for j in X[i]['terminal']])\n",
    "    neuronvis.render.setPointSize(1)\n",
    "neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for ii,i in enumerate(X):\n",
    "    print(i)\n",
    "    neuronvis = nv.neuronVis(size=(1200,900))\n",
    "    neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "    neuronvis.addRegion('SCm')\n",
    "    draw_points(neuronvis,X[i]['terminal'],[[1,0,0] if ii==0 else [0,0,1] for j in X[i]['terminal']])\n",
    "    neuronvis.render.setPointSize(1)\n",
    "    neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "for ii,i in enumerate(X):\n",
    "    draw_points(neuronvis,X[i]['terminal'],[[1,0,0] for j in X[i]['terminal']])\n",
    "    neuronvis.render.setPointSize(1)\n",
    "neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mask = get_region_mask('SCm')\n",
    "X = {'VISp':{'terminal':[]},'SSp-bfd':{'terminal':[]}}\n",
    "for id,neuron in enumerate(neuron_to_brainstem_information):\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']+neuron_to_brainstem_information[neuron]['cortex_subregion']\n",
    "    if region not in X:\n",
    "        continue\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            X[region]['terminal'].append(terminal.copy())\n",
    "            if neuron_terminals[neuron]['mirror_mark']:\n",
    "                X[region]['terminal'][-1][2] = 11400-X[region]['terminal'][-1][2]\n",
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "for ii,i in enumerate(X):\n",
    "    neuronvis.addRegion('SCm')\n",
    "    draw_points(neuronvis,X[i]['terminal'],[[1,0,0] if ii==0 else [0,0,1] for j in X[i]['terminal']])\n",
    "    neuronvis.render.setPointSize(1)\n",
    "neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mask = get_region_mask('SCm')\n",
    "X = {'TEa':{'terminal':[]},'SSp-bfd':{'terminal':[]}}\n",
    "for id,neuron in enumerate(neuron_to_brainstem_information):\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']+neuron_to_brainstem_information[neuron]['cortex_subregion']\n",
    "    if region not in X:\n",
    "        continue\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            X[region]['terminal'].append(terminal.copy())\n",
    "            if neuron_terminals[neuron]['mirror_mark']:\n",
    "                X[region]['terminal'][-1][2] = 11400-X[region]['terminal'][-1][2]\n",
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "for ii,i in enumerate(X):\n",
    "    neuronvis.addRegion('SCm')\n",
    "    draw_points(neuronvis,X[i]['terminal'],[[1,0,0] if ii==0 else [0,0,1] for j in X[i]['terminal']])\n",
    "    neuronvis.render.setPointSize(1)\n",
    "neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### AUDs to SCm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "source = ['AUDp','AUDv','AUDd','AUDpo']\n",
    "target = ['ICd','ICe','ICc','SCzo','SCsg','SCop','SCig','SCiw','SCdg','SCdw','PAG']\n",
    "X = {}\n",
    "for neuron in tool_bar(cortex_neuron_name,desc='iteration on neurons (generating mapping colors) ...',leave=False,colour='#55AF7B'):\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']\n",
    "    if region!='AUD':\n",
    "        continue\n",
    "    region += neuron_to_brainstem_information[neuron]['cortex_subregion']\n",
    "    if region not in X:\n",
    "        X[region] = []\n",
    "    X[region].append(get_axonal_statics_by_regions(neuron,target,'terminalregion'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Y = []\n",
    "for i in source:\n",
    "    Y.append(np.mean(X[i],axis=0))\n",
    "    Y[-1] = Y[-1]/np.linalg.norm(Y[-1])\n",
    "Y = np.array(Y)\n",
    "plt.matshow(Y,cmap='BuGn')\n",
    "plt.xticks(range(len(target)),target,fontsize=fontsize_ticks,rotation=-45)\n",
    "plt.yticks(range(len(source)),source,fontsize=fontsize_ticks)\n",
    "plt.xlabel('Target',fontsize=fontsize_label)\n",
    "plt.ylabel('Source',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.title(\"Terminal distribution (normalized mean)\",fontsize=fontsize_title)\n",
    "plt.colorbar()\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(midbrain_path+'AUD_branches.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Primary and Secondary sensory cortices difference in the midbrain"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = []\n",
    "source = ['VISp','VISs','SSp','SSs','AUDs','AUDp']\n",
    "target = ['SCs','SCm','IC']\n",
    "info = {}\n",
    "for i in neuron_to_brainstem_information:\n",
    "    region = neuron_to_brainstem_information[i]['cortex_region']\n",
    "    if region not in ['VIS','PTLp','SSp','SSs','AUD']:\n",
    "        continue\n",
    "    if region in ['VIS','AUD']:\n",
    "        region += 'p' if neuron_to_brainstem_information[i]['cortex_subregion']=='p' else 's'\n",
    "    elif region in ['PTLp']:\n",
    "        region = 'VISs'\n",
    "    else:\n",
    "        region = region\n",
    "    if region not in info:\n",
    "        info[region] = []\n",
    "    info[region].append(i)\n",
    "for i in source:\n",
    "    neuron_info = get_neuron_axon_info(info[i],terminal_or_length='terminal',if_combined=True,if_log=False,filter=target)\n",
    "    a = np.sum(neuron_info[1],axis=0)\n",
    "    result.append(a/np.linalg.norm(a))\n",
    "plt.matshow(np.array(result).T,cmap='BuGn')\n",
    "_ = plt.yticks(range(len(target)),target,fontsize=fontsize_ticks)\n",
    "_ = plt.xticks(range(len(source)),source,fontsize=fontsize_ticks)\n",
    "plt.xlabel('Modality',fontsize=fontsize_label)\n",
    "plt.ylabel('Terminal number (normalized)',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.title('Projection preference',fontsize=fontsize_title)\n",
    "plt.savefig(midbrain_path+'Modality_primary_secondary_difference.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.colorbar()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = []\n",
    "source = []\n",
    "target = ['SCs','SCm','IC']\n",
    "info = {}\n",
    "for i in neuron_to_brainstem_information:\n",
    "    region = neuron_to_brainstem_information[i]['cortex_region']\n",
    "    if region not in ['VIS','PTLp','SSp','SSs','AUD']:\n",
    "        continue\n",
    "    if region in ['VIS','PTLp','AUD']:\n",
    "        region += neuron_to_brainstem_information[i]['cortex_subregion']\n",
    "    else:\n",
    "        region = region\n",
    "    if region not in info:\n",
    "        info[region] = []\n",
    "        source.append(region)\n",
    "    info[region].append(i)\n",
    "for i in source:\n",
    "    neuron_info = get_neuron_axon_info(info[i],terminal_or_length='terminal',if_combined=True,if_log=False,filter=target)\n",
    "    a = np.sum(neuron_info[1],axis=0)\n",
    "    result.append(a/np.linalg.norm(a))\n",
    "tmp = [[i,j] for i,j in zip(source,result)]\n",
    "tmp = sorted(tmp,key=lambda x:x[1][0]*3+x[1][1]*1+x[1][2]*0,reverse=True)\n",
    "source = []\n",
    "result = []\n",
    "for i in tmp:\n",
    "    source.append(i[0])\n",
    "    result.append(i[1])\n",
    "plt.figure(figsize=(5,4))\n",
    "plt.matshow(np.array(result).T,cmap='BuGn')\n",
    "_ = plt.yticks(range(len(target)),target,fontsize=fontsize_ticks)\n",
    "_ = plt.xticks(range(len(source)),source,fontsize=fontsize_ticks)\n",
    "plt.xlabel('Modality',fontsize=fontsize_label)\n",
    "plt.ylabel('Terminal number (normalized)',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.title('Projection preference',fontsize=fontsize_title)\n",
    "plt.savefig(midbrain_path+'Modality_primary_secondary_difference_detaile.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.colorbar()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = []\n",
    "source = []\n",
    "target = ['SCzo','SCsg','SCop','SCig','SCiw','SCdg','SCdw','ICd','ICe','ICc','PAG']\n",
    "info = {}\n",
    "integration_system_info = {}\n",
    "for i in neuron_to_brainstem_information:\n",
    "    region = neuron_to_brainstem_information[i]['cortex_region']+neuron_to_brainstem_information[i]['cortex_subregion']\n",
    "    if region not in info:\n",
    "        info[region] = []\n",
    "        source.append(region)\n",
    "    info[region].append(i)\n",
    "    integration_system_info[i] = get_axonal_statics_by_regions(i,target,'terminalregion')\n",
    "for i in source:\n",
    "    neuron_info = get_axonal_statics_by_regions(i,brainstem_region,'terminalregion')\n",
    "    a = np.sum(neuron_info[1],axis=0)\n",
    "    result.append(a/np.linalg.norm(a))\n",
    "tmp = [[i,j] for i,j in zip(source,result)]\n",
    "tmp = sorted(tmp,key=lambda x:x[1][0]*3+x[1][1]*1+x[1][2]*0,reverse=True)\n",
    "source = []\n",
    "result = []\n",
    "for i in tmp:\n",
    "    source.append(i[0])\n",
    "    result.append(i[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize=(5,4))\n",
    "plt.matshow(np.array(result).T,cmap='BuGn')\n",
    "_ = plt.yticks(range(len(target)),target,fontsize=fontsize_ticks)\n",
    "_ = plt.xticks(range(len(source)),source,fontsize=fontsize_ticks)\n",
    "plt.xlabel('Modality',fontsize=fontsize_label)\n",
    "plt.ylabel('Terminal number (normalized)',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.title('Projection preference',fontsize=fontsize_title)\n",
    "plt.savefig(midbrain_path+'Modality_primary_secondary_difference_detaile.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.colorbar()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp = {}\n",
    "for i in info:\n",
    "    tmp[i] = []\n",
    "    tmp[i]=get_neuron_axon_info(info[i],terminal_or_length='terminal',if_combined=True,if_log=False,filter=target)[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = []\n",
    "Y = []\n",
    "_r = ['VISp','SSp','AUDp','VISrl','VISl','AUDd','VISal']\n",
    "color = np.random.rand(20,3)\n",
    "for i in tmp:\n",
    "    if i not in _r:\n",
    "        continue\n",
    "    X += tmp[i].tolist()\n",
    "    for j in tmp[i]:\n",
    "        Y.append(color_pool[_r.index(i)])\n",
    "X = np.array(X)\n",
    "Y = np.array(Y)\n",
    "mask = np.max(X,axis=-1)>3\n",
    "from sklearn import manifold\n",
    "tsne = manifold.TSNE(n_components=2, random_state=0)\n",
    "tsne_data = tsne.fit_transform(X[mask])\n",
    "plt.scatter(tsne_data[:,0],tsne_data[:,1],c=Y[mask],s=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.scatter(tsne_data[:,0],tsne_data[:,1],s=0.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp = [0,0,0]\n",
    "tmp2 = {0:[],1:[],2:[]}\n",
    "for i,j in zip(source,result):\n",
    "    tmp[np.argmax(j)] += 1\n",
    "    tmp2[np.argmax(j)].append(i)\n",
    "plt.figure(figsize=(4,5))\n",
    "plt.bar(range(len(tmp)),tmp,width=0.25,color=color_pool[0])\n",
    "plt.xticks(range(len(target)),target,fontsize=fontsize_ticks)\n",
    "plt.xlim(-0.5,len(target)-0.5)\n",
    "plt.xlabel('Target',fontsize=fontsize_label)\n",
    "plt.ylabel('Terminal number',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.title(\"Dopaminergic system\",fontsize=fontsize_title)\n",
    "plt.savefig(midbrain_path+'Sensory_center.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()\n",
    "tmp2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "source = ['VISp','SSp','AUDp','VISrl','VISl','AUDd','VISal']\n",
    "target = ['SCs','SCm','IC']\n",
    "info = {}\n",
    "for i in neuron_to_brainstem_information:\n",
    "    region = neuron_to_brainstem_information[i]['cortex_region']\n",
    "    if region in ['VIS','PTLp','AUD']:\n",
    "        region += neuron_to_brainstem_information[i]['cortex_subregion']\n",
    "    if region=='PTLprl':\n",
    "        region = 'VISrl'\n",
    "    if region not in source:\n",
    "        continue\n",
    "    if region not in info:\n",
    "        info[region] = []\n",
    "    info[region].append(i)\n",
    "for i in info:\n",
    "    neuron_info = get_neuron_axon_info(info[i],terminal_or_length='terminal',if_combined=True,if_log=False,filter=target)\n",
    "    a = np.mean(neuron_info[1],axis=0)\n",
    "    a /= np.linalg.norm(a)\n",
    "    info[i] = [len(info[i]),a]\n",
    "color_transform = {'VISrl':1,'VISl':3,'AUDd':4,'VISal':0}\n",
    "ax = plt.figure().add_subplot(projection='3d')\n",
    "ax.plot([0,1],[0,0],[0,0],color='#AAAAAA')\n",
    "ax.plot([0,0],[0,1],[0,0],color='#AAAAAA')\n",
    "ax.plot([0,0],[0,0],[0,1],color='#AAAAAA')\n",
    "for i in info:\n",
    "    if i in color_transform:\n",
    "        c = color_pool[color_transform[i]]\n",
    "    else:\n",
    "        c = np.array([0,0,0])\n",
    "    ax.quiver([0],[0],[0],info[i][1][0],info[i][1][1],info[i][1][2],color=c,label=i,arrow_length_ratio=0.15)\n",
    "    ax.text(info[i][1][0],info[i][1][1],info[i][1][2],i,color=c)\n",
    "ax.view_init(elev=25,azim=45,vertical_axis='z')\n",
    "ax.set_xlabel('SCs',fontsize=fontsize_label)\n",
    "ax.set_ylabel('SCm',fontsize=fontsize_label)\n",
    "ax.set_zlabel('IC',fontsize=fontsize_label)\n",
    "ax.set_title('Secondary sensory difference',fontsize=fontsize_title)\n",
    "ax.set_xticks([],[])\n",
    "ax.set_yticks([],[])\n",
    "ax.set_zticks([],[])\n",
    "ax.grid(None)\n",
    "plt.savefig(midbrain_path+'Secondary_sensory_difference.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "for i,j,k in zip(['VISp','VISrl','SSp'],['233666208.swc','221426059.swc','221512067.swc'],[3,1,0]):\n",
    "    neuron_tree = iondata.getNeuronTreeByID(j[:-7],j[-7:])\n",
    "    neuron_tree.somaRadius = 50\n",
    "    neuronvis.addNeuronTree(neuron_tree,axonColor=color_pool[k],somaColor=[0.5,0.5,0.5],dendriteColor=[0.5,0.5,0.5],mirrorToRight=True,isLine=False)\n",
    "for l in ['MB']:\n",
    "    neuronvis.addRegion(l)\n",
    "set_view(neuronvis,'right')\n",
    "neuronvis.render.savepng(midbrain_path+'Transition_example.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "for i,j,k in zip(['AUDp','AUDd','SSp'],['221521026.swc','221739074.swc','221071040.swc'],[3,1,0]):\n",
    "    neuron_tree = iondata.getNeuronTreeByID(j[:-7],j[-7:])\n",
    "    neuron_tree.somaRadius = 50\n",
    "    neuronvis.addNeuronTree(neuron_tree,axonColor=color_pool[k],somaColor=[0.5,0.5,0.5],dendriteColor=[0.5,0.5,0.5],mirrorToRight=True,isLine=False)\n",
    "for l in ['MB']:\n",
    "    neuronvis.addRegion(l)\n",
    "set_view(neuronvis,'dorsal')\n",
    "neuronvis.render.savepng(midbrain_path+'Transition_example_AUDd.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### sensory and motor relationship"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.matshow((np.sum(MB_mask,axis=-1)>0).T)\n",
    "plt.plot([1178,750],[0,800],color='y')\n",
    "plt.plot([1000,700],[0,800],color='b')\n",
    "plt.plot([780,780],[0,800],color='g')\n",
    "#plt.xlim(0,MB_mask.shape[1])\n",
    "#plt.ylim(0,MB_mask.shape[0])\n",
    "plt.yticks([],[])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "neuronvis.addRegion('MB')\n",
    "set_view(neuronvis,'left')\n",
    "neuronvis.render.savepng(midbrain_path+'midbrain_segmentation_line.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### pretectum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = ['VIS','ACA','SSp','MOs','AUD','TEa','AI']\n",
    "X = {}\n",
    "segment_line = {i:[np.array([780,0]),np.array([780,800])] for i in target}\n",
    "segment_range = 20\n",
    "for neuron in tool_bar(cortex_neuron_name,desc='iteration on neurons (generating mapping colors) ...',leave=False,colour='#55AF7B'):\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']\n",
    "    if region not in target:\n",
    "        continue\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        if get_mask_value(MB_mask,np.array(terminal)/10)==0:\n",
    "            continue\n",
    "        p = np.array([int(terminal[0]/10),int(terminal[1]/10),1140-int(terminal[2]/10) if neuron_terminals[neuron]['mirror_mark'] else int(terminal[2]/10)])\n",
    "        p_projection = project_point_on_line_segment(p[:2],segment_line[region][0],segment_line[region][1])\n",
    "        d = np.linalg.norm(p[:2]-p_projection)\n",
    "        if d>segment_range:\n",
    "            continue\n",
    "        p = [p[2],np.linalg.norm(segment_line[region][0]-p_projection)]\n",
    "        if region not in X:\n",
    "            X[region] = []\n",
    "        X[region].append(p)\n",
    "mask_list = [MB_mask]+[get_region_mask(i) for i in ['APN','MRN','PAG','SNr','VTA']]\n",
    "for e in [['VIS','ACA'],['SSp','MOs'],['AUD','TEa','AI']]:\n",
    "    plt.figure(figsize=(10,10))\n",
    "    plt.imshow(np.sum([get_contour(np.sum(i[800-5:800+5],axis=0)>0) for i in mask_list],axis=0)>0,cmap='Greys')\n",
    "    for i,c in zip(e,[3,0,1]):\n",
    "        j = np.array(X[i])\n",
    "        plt.scatter(j[:,0],j[:,1],label=i,color=color_pool[c],s=0.25,alpha=0.5)\n",
    "    plt.axis('off')\n",
    "    plt.axis(\"equal\")\n",
    "    plt.legend()\n",
    "    plt.title('_'.join(e))\n",
    "    plt.savefig(midbrain_path+'sensory_motor_relationship_pretectum_'+'_'.join(e)+'.pdf',format='pdf')\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### m1 and m2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = ['VIS','ACA','SSp','MOs','AUD','TEa','AI']\n",
    "X = {}\n",
    "segment_line = {\n",
    "    'VIS':[np.array([1000,0]),np.array([700,800])],'ACA':[np.array([1000,0]),np.array([700,800])],\n",
    "    'SSp':[np.array([1000,0]),np.array([700,800])],'MOs':[np.array([1000,0]),np.array([700,800])],\n",
    "    'AUD':[np.array([1178,0]),np.array([750,800])],'TEa':[np.array([1178,0]),np.array([750,800])],'AI':[np.array([1178,0]),np.array([750,800])]}\n",
    "segment_range = 20\n",
    "for neuron in tool_bar(cortex_neuron_name,desc='iteration on neurons (generating mapping colors) ...',leave=False,colour='#55AF7B'):\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']\n",
    "    if region not in target:\n",
    "        continue\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        if get_mask_value(MB_mask,np.array(terminal)/10)==0:\n",
    "            continue\n",
    "        p = np.array([int(terminal[0]/10),int(terminal[1]/10),1140-int(terminal[2]/10) if neuron_terminals[neuron]['mirror_mark'] else int(terminal[2]/10)])\n",
    "        p_projection = project_point_on_line_segment(p[:2],segment_line[region][0],segment_line[region][1])\n",
    "        d = np.linalg.norm(p[:2]-p_projection)\n",
    "        if d>segment_range:\n",
    "            continue\n",
    "        p = [p[2],np.linalg.norm(segment_line[region][0]-p_projection)]\n",
    "        if region not in X:\n",
    "            X[region] = []\n",
    "        X[region].append(p)\n",
    "mask_list = [MB_mask]+[get_region_mask(i) for i in ['SCm','MRN','PAG','RN','IC','SNr','VTA']]\n",
    "for e in [['VIS','ACA'],['SSp','MOs'],['AUD','TEa','AI']]:\n",
    "    plt.figure(figsize=(10,10))\n",
    "    tmp = segment_line[e[0]][1]-segment_line[e[0]][0]\n",
    "    tmp = tmp/np.linalg.norm(tmp)\n",
    "    m = [[] for i in mask_list]\n",
    "    start = (segment_line[e[0]][0].copy()).astype(float)\n",
    "    while True:\n",
    "        try:\n",
    "            for i in range(len(mask_list)):\n",
    "                m[i].append(mask_list[i][int(np.round(start[0])),int(np.round(start[1]))])\n",
    "            start = start+tmp\n",
    "        except:\n",
    "            break\n",
    "    m = np.sum([get_contour(i) for i in m],axis=0)>0\n",
    "    plt.imshow(m,cmap='Greys')\n",
    "    for i,c in zip(e,[3,0,1]):\n",
    "        j = np.array(X[i])\n",
    "        plt.scatter(j[:,0],j[:,1],label=i,color=color_pool[c],s=0.25,alpha=0.5)\n",
    "    plt.axis('off')\n",
    "    plt.axis(\"equal\")\n",
    "    plt.legend()\n",
    "    plt.title('_'.join(e))\n",
    "    plt.savefig(midbrain_path+'sensory_motor_relationship_m1_m2_'+'_'.join(e)+'.pdf',format='pdf')\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = []\n",
    "X = {}\n",
    "segment_line = {\n",
    "    'VIS':[np.array([1000,0]),np.array([700,800])],'ACA':[np.array([1000,0]),np.array([700,800])],\n",
    "    'SSp':[np.array([1000,0]),np.array([700,800])],'MOs':[np.array([1000,0]),np.array([700,800])],\n",
    "    'AUD':[np.array([1178,0]),np.array([750,800])],'TEa':[np.array([1178,0]),np.array([750,800])],'AI':[np.array([1178,0]),np.array([750,800])]}\n",
    "segment_range = 20\n",
    "for neuron in tool_bar(cortex_neuron_name,desc='iteration on neurons (generating mapping colors) ...',leave=False,colour='#55AF7B'):\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']\n",
    "    subregion = neuron_to_brainstem_information[neuron]['cortex_subregion']\n",
    "    if region not in ['SSp']:\n",
    "        continue\n",
    "    if subregion not in target:\n",
    "        target.append(subregion)\n",
    "        X[subregion] = []\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        if get_mask_value(MB_mask,np.array(terminal)/10)==0:\n",
    "            continue\n",
    "        p = np.array([int(terminal[0]/10),int(terminal[1]/10),1140-int(terminal[2]/10) if neuron_terminals[neuron]['mirror_mark'] else int(terminal[2]/10)])\n",
    "        p_projection = project_point_on_line_segment(p[:2],segment_line[region][0],segment_line[region][1])\n",
    "        d = np.linalg.norm(p[:2]-p_projection)\n",
    "        if d>segment_range:\n",
    "            continue\n",
    "        p = [p[2],np.linalg.norm(segment_line[region][0]-p_projection)]\n",
    "        X[subregion].append(p)\n",
    "mask_list = [MB_mask]+[get_region_mask(i) for i in ['SCm','MRN','PAG','RN','IC','SNr','VTA']]\n",
    "for e in [target]:\n",
    "    plt.figure(figsize=(10,10))\n",
    "    tmp = segment_line['SSp'][1]-segment_line['SSp'][0]\n",
    "    tmp = tmp/np.linalg.norm(tmp)\n",
    "    m = [[] for i in mask_list]\n",
    "    start = (segment_line['SSp'][0].copy()).astype(float)\n",
    "    while True:\n",
    "        try:\n",
    "            for i in range(len(mask_list)):\n",
    "                m[i].append(mask_list[i][int(np.round(start[0])),int(np.round(start[1]))])\n",
    "            start = start+tmp\n",
    "        except:\n",
    "            break\n",
    "    m = np.sum([get_contour(i) for i in m],axis=0)>0\n",
    "    plt.imshow(m,cmap='Greys')\n",
    "    for i,c in zip(e,range(len(e))):\n",
    "        j = np.array(X[i])\n",
    "        plt.scatter(j[:,0],j[:,1],label=i,color=color_pool[c],s=0.25,alpha=0.5)\n",
    "    plt.axis('off')\n",
    "    plt.axis(\"equal\")\n",
    "    plt.legend()\n",
    "    plt.title('_'.join(e))\n",
    "    plt.savefig(midbrain_path+'SSp_subregions_relationship_m1_m2.pdf',format='pdf')\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = []\n",
    "X = {}\n",
    "segment_range = 20\n",
    "for neuron in tool_bar(cortex_neuron_name,desc='iteration on neurons (generating mapping colors) ...',leave=False,colour='#55AF7B'):\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']\n",
    "    subregion = neuron_to_brainstem_information[neuron]['cortex_subregion']\n",
    "    if region not in ['SSp','VIS']:\n",
    "        continue\n",
    "    if region=='SSp':\n",
    "        region += subregion\n",
    "    if region not in target:\n",
    "        target.append(region)\n",
    "        X[region] = []\n",
    "    X[region].append(neuron)\n",
    "Y = {}\n",
    "region_list = brainstem_region_hierachy['Cortex'].copy()\n",
    "for i in X:\n",
    "    Y[i] = get_neuron_axon_info(X[i],terminal_or_length='terminal',if_combined = True,if_log = False,filter=region_list)[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp = []\n",
    "y_labels = ['VIS','SSp-bfd','SSp-tr','SSp-ll','SSp-ul','SSp-un','SSp-n','SSp-m']\n",
    "for i in y_labels:\n",
    "    tmp.append(np.mean(Y[i],axis=0))\n",
    "tmp = np.array(tmp)\n",
    "tmp[1:,-4] *= 0\n",
    "tmp[0,-1] *= 0\n",
    "plt.matshow(tmp,cmap='BuGn')\n",
    "_ = plt.xticks(range(len(region_list)),region_list,fontsize=fontsize_ticks,rotation=-45)\n",
    "_ = plt.yticks(range(len(y_labels)),y_labels,fontsize=fontsize_ticks)\n",
    "plt.xlabel('Target',fontsize=fontsize_label)\n",
    "plt.ylabel('Source',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.title(\"Terminal distribution (mean)\",fontsize=fontsize_title)\n",
    "plt.colorbar()\n",
    "plt.savefig(midbrain_path+'SSp_VIS_cortical_projection.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear()\n",
    "for i,j in zip(['221475006.swc','220195092.swc'],[1,0]):\n",
    "    neuronvis.addNeuronByID(i[:-7],i[-7:],axonColor=color_pool[j],somaColor=color_pool[j],dendriteHide=True,mirrorToRight=True)\n",
    "neuronvis.render.savepng(midbrain_path+'identification_movement_example.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### MOs also topograpically distribution in MRN"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuron_color_mapping = []\n",
    "name = []\n",
    "for i in tool_bar(cortex_neuron_name,desc='iteration on neurons (generating mapping colors) ...',leave=False,colour='#55AF7B'):\n",
    "    if neuron_to_brainstem_information[i]['cortex_region'] not in ['MOs']:\n",
    "        continue\n",
    "    neuron_color_mapping.append(neuron_to_brainstem_information[i]['soma_flatten_position'])\n",
    "    name.append(i)\n",
    "tmp = position_color_mapping(np.array(neuron_color_mapping),2)\n",
    "neuron_color_mapping = {}\n",
    "for i,j in zip(name,tmp):\n",
    "    neuron_color_mapping[i] = j"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = ['SCm','MRN']\n",
    "mask = 0.0\n",
    "for i,j in enumerate(target):\n",
    "    mask = np.where(get_region_mask(j),i+1,mask)\n",
    "X = {}\n",
    "for id,neuron in enumerate(name):\n",
    "    X[neuron] = {'color':neuron_color_mapping[neuron],'terminal':[]}\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            X[neuron]['terminal'].append(terminal.copy())\n",
    "            if neuron_terminals[neuron]['mirror_mark']:\n",
    "                X[neuron]['terminal'][-1][2] = 11400-X[neuron]['terminal'][-1][2]\n",
    "mask_2D = np.sum(mask,axis=0)\n",
    "a,b = get_value_range(mask_2D)\n",
    "tmp = [np.mean(a),np.mean(b)]\n",
    "tmp2 = np.max([a[1]-a[0],b[1]-b[0]])\n",
    "_r = [tmp[0]-tmp2/2,tmp[0]+tmp2/2,tmp[1]+tmp2/2,tmp[1]-tmp2/2]\n",
    "m = 1-get_contour(mask_2D>0)\n",
    "T = {}\n",
    "for i,j in enumerate(target):\n",
    "    m = np.where(get_contour(np.sum(mask==i+1,axis=0)>0),0,m)\n",
    "    tmp = np.sum(mask==i+1,axis=0)\n",
    "    tmp[...,:570] *= 0\n",
    "    a,b = get_value_range(tmp)\n",
    "    T[j] = [np.mean(a),np.mean(b)]\n",
    "fig,ax = plt.subplots(figsize=(5,5))\n",
    "ax.matshow(m,cmap='gray')\n",
    "for i in X:\n",
    "    if len(X[i]['terminal'])==0:\n",
    "        continue\n",
    "    tmp = np.array(X[i]['terminal'])\n",
    "    ax.scatter(tmp[:,2]/10,tmp[:,1]/10,color=X[i]['color'],s=0.1)\n",
    "for i in T:\n",
    "    ax.text(T[i][0]-10,T[i][1],i,fontsize=fontsize_ticks)\n",
    "ax.set_xlim(_r[0],_r[1])\n",
    "ax.set_ylim(_r[2],_r[3])\n",
    "ax.set_title(\"Terminal in SCm and MRN\\n(cronal view)\",fontsize=fontsize_title)\n",
    "ax.set_axis_off()\n",
    "#ax.legend(handles=[mpatches.Patch(color=_color[i], label=i) for i in _color],loc='lower right')\n",
    "plt.savefig(midbrain_path+'Terminal_in_SCm_MRN_MOs_cronal.pdf',format='pdf',bbox_inches='tight',dpi=100)\n",
    "mask_2D = np.sum(mask,axis=-1).T\n",
    "a,b = get_value_range(mask_2D)\n",
    "tmp = [np.mean(a),np.mean(b)]\n",
    "tmp2 = np.max([a[1]-a[0],b[1]-b[0]])\n",
    "_r = [tmp[0]-tmp2/2,tmp[0]+tmp2/2,tmp[1]+tmp2/2,tmp[1]-tmp2/2]\n",
    "m = 1-get_contour(mask_2D>0)\n",
    "T = {}\n",
    "for i,j in enumerate(target):\n",
    "    m = np.where(get_contour(np.sum(mask==i+1,axis=-1).T>0),0,m)\n",
    "    tmp = np.sum(mask==i+1,axis=-1).T\n",
    "    #tmp[...,:570] *= 0\n",
    "    a,b = get_value_range(tmp)\n",
    "    T[j] = [np.mean(a),np.mean(b)]\n",
    "fig,ax = plt.subplots(figsize=(5,5))\n",
    "ax.matshow(m,cmap='gray')\n",
    "for i in X:\n",
    "    if len(X[i]['terminal'])==0:\n",
    "        continue\n",
    "    tmp = np.array(X[i]['terminal'])\n",
    "    ax.scatter(tmp[:,0]/10,tmp[:,1]/10,color=X[i]['color'],s=0.1)\n",
    "for i in T:\n",
    "    ax.text(T[i][0]-10,T[i][1],i,fontsize=fontsize_ticks)\n",
    "ax.set_xlim(_r[0],_r[1])\n",
    "ax.set_ylim(_r[2],_r[3])\n",
    "ax.set_title(\"Terminal in SCm and MRN\\n(sagittal view)\",fontsize=fontsize_title)\n",
    "ax.set_axis_off()\n",
    "#ax.legend(handles=[mpatches.Patch(color=_color[i], label=i) for i in _color],loc='lower right')\n",
    "plt.savefig(midbrain_path+'Terminal_in_SCm_MRN_MOs_sagittal.pdf',format='pdf',bbox_inches='tight',dpi=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "flatmap = _flatmap*1\n",
    "for i in tool_bar(name):\n",
    "        l = neuron_to_brainstem_information[i]['soma_flatten_position']\n",
    "        cv.circle(flatmap,[int(l[0])*2, int(l[1])*2],8,np.array(neuron_color_mapping[i])*255,-1)\n",
    "fig = plt.figure(figsize=(10,10))\n",
    "plt.imshow(flatmap)\n",
    "plt.xlim(300,2700)\n",
    "plt.ylim(2700,300)\n",
    "plt.axis(\"equal\")\n",
    "plt.axis('off')\n",
    "plt.title('Neuron coding',fontsize=fontsize_title)\n",
    "plt.savefig(midbrain_path+'Terminal_in_SCm_MRN_MOs.pdf',format='pdf',bbox_inches='tight',dpi=100)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### SCm and MRN"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = get_neuron_axon_info(name,terminal_or_length='terminal',if_combined=True,if_log=False,filter=['SCm','MRN'])[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "n = np.expand_dims(np.max(X,axis=-1),axis=-1)\n",
    "n = np.where(n==0,1,n)\n",
    "Y = X/n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.sum(np.sum(Y,axis=-1)<1.5)/len(Y)*100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "(1-0.75**2)**0.5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "seaborn.clustermap(Y.T>0.75)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "seaborn.clustermap(Y.T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "flatmap = _flatmap*1\n",
    "for i,j in zip(name,Y):\n",
    "        l = neuron_to_brainstem_information[i]['soma_flatten_position']\n",
    "        cv.circle(flatmap,[int(l[0])*2, int(l[1])*2],15,np.array([j[0],0,j[1]])*255,-1)\n",
    "fig = plt.figure(figsize=(10,10))\n",
    "plt.imshow(flatmap)\n",
    "plt.xlim(300,2700)\n",
    "plt.ylim(2700,300)\n",
    "plt.axis(\"equal\")\n",
    "plt.axis('off')\n",
    "plt.title('Neuron coding',fontsize=fontsize_title)\n",
    "#plt.savefig(midbrain_path+'Terminal_in_SCm_MRN_MOs.pdf',format='pdf',bbox_inches='tight',dpi=100)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "name[44]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "name[21]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "X[name.index('220189002.swc')]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### sensory and motor layer difference"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "source = ['VIS','ACA','SS','MO','AUD','TEa','AI']\n",
    "target = ['ICd','ICe','ICc','SCzo','SCsg','SCop','SCig','SCiw','SCdg','SCdw','PAG','MRN','III','EW','RN','CUN','PPN','SNr','VTA','RAmb']\n",
    "X = {}\n",
    "for neuron in tool_bar(cortex_neuron_name,desc='iteration on neurons (generating mapping colors) ...',leave=False,colour='#55AF7B'):\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']\n",
    "    if region in ['SSp','SSs']:\n",
    "        region = 'SS'\n",
    "    if region in ['MOp','MOs']:\n",
    "        region = 'MO'\n",
    "    if region not in source:\n",
    "        continue\n",
    "    if region not in X:\n",
    "        X[region] = []\n",
    "    X[region].append(get_axonal_statics_by_regions(neuron,target,'terminalregion'))\n",
    "Y = []\n",
    "for i in source:\n",
    "    Y.append(np.mean(X[i],axis=0))\n",
    "    Y[-1] = Y[-1]/np.linalg.norm(Y[-1])\n",
    "Y = np.array(Y)\n",
    "plt.matshow(Y,cmap='BuGn')\n",
    "plt.axhline(1.5,color='#888888',linestyle='--')\n",
    "plt.axhline(3.5,color='#888888',linestyle='--')\n",
    "plt.plot(np.argmax(Y[:2],axis=1),[0,1],color=color_pool[0],marker='o')\n",
    "plt.plot(np.argmax(Y[2:4],axis=1),[2,3],color=color_pool[0],marker='o')\n",
    "plt.plot(np.argmax(Y[4:],axis=1),[4,5,6],color=color_pool[0],marker='o')\n",
    "plt.xticks(range(len(target)),target,fontsize=fontsize_ticks,rotation=-45)\n",
    "plt.yticks(range(len(source)),source,fontsize=fontsize_ticks)\n",
    "plt.xlabel('Target',fontsize=fontsize_label)\n",
    "plt.ylabel('Source',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.title(\"Terminal distribution (normalized mean)\",fontsize=fontsize_title)\n",
    "plt.colorbar()\n",
    "plt.savefig(midbrain_path+'Sensory_Motor_vector.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### motor, dopaminergic, raphe system"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = ['PAG','MRN','III','RN','CUN','PPN']\n",
    "info = get_neuron_axon_info(cortex_neuron_name,terminal_or_length='terminal',if_combined=True,if_log=False,filter=target)\n",
    "X = [np.sum(info[1][:,:2]),np.sum(info[1][:,2:])]\n",
    "plt.figure(figsize=(3,5))\n",
    "plt.bar(range(2),X,width=0.25,color=color_pool[0])\n",
    "plt.xticks([0,1],['PAG+MRN','III+RN'],fontsize=fontsize_ticks)\n",
    "plt.xlim(-0.5,1.5)\n",
    "plt.xlabel('Target',fontsize=fontsize_label)\n",
    "plt.ylabel('Terminal number',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.title(\"Motor system\",fontsize=fontsize_title)\n",
    "plt.savefig(midbrain_path+'PAG+MRN_III+RN.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = ['PAG','MRN','III','RN','CUN','PPN']\n",
    "info = get_neuron_axon_info(cortex_neuron_name,terminal_or_length='terminal',if_combined=True,if_log=False,filter=target)\n",
    "X = [np.sum(info[1][:,i]) for i in range(len(target))]\n",
    "plt.figure(figsize=(4,5))\n",
    "plt.bar(range(len(target)),X,width=0.25,color=color_pool[0])\n",
    "plt.xticks(range(len(target)),target,fontsize=fontsize_ticks)\n",
    "plt.xlim(-0.5,len(target)-0.5)\n",
    "plt.xlabel('Target',fontsize=fontsize_label)\n",
    "plt.ylabel('Terminal number',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.title(\"Motor system\",fontsize=fontsize_title)\n",
    "plt.savefig(midbrain_path+'Motor_system.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = ['SNc','SNr','VTA']\n",
    "info = get_neuron_axon_info(cortex_neuron_name,terminal_or_length='terminal',if_combined=True,if_log=False,filter=target)\n",
    "X = [np.sum(info[1][:,i]) for i in range(len(target))]\n",
    "plt.figure(figsize=(4,5))\n",
    "plt.bar(range(len(target)),X,width=0.25,color=color_pool[0])\n",
    "plt.xticks(range(len(target)),target,fontsize=fontsize_ticks)\n",
    "plt.xlim(-0.5,len(target)-0.5)\n",
    "plt.xlabel('Target',fontsize=fontsize_label)\n",
    "plt.ylabel('Terminal number',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.title(\"Dopaminergic system\",fontsize=fontsize_title)\n",
    "plt.savefig(midbrain_path+'Dopaminergic_system.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = ['IF','IPN','RL','CLI','DR']\n",
    "info = get_neuron_axon_info(cortex_neuron_name,terminal_or_length='terminal',if_combined=True,if_log=False,filter=target)\n",
    "X = [np.sum(info[1][:,i]) for i in range(len(target))]\n",
    "plt.figure(figsize=(6,5))\n",
    "plt.bar(range(len(target)),X,width=0.25,color=color_pool[0])\n",
    "plt.xticks(range(len(target)),target,fontsize=fontsize_ticks)\n",
    "plt.xlim(-0.5,len(target)-0.5)\n",
    "plt.xlabel('Target',fontsize=fontsize_label)\n",
    "plt.ylabel('Terminal number',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.title(\"Raphe system\",fontsize=fontsize_title)\n",
    "plt.savefig(midbrain_path+'Raphe_system.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### MRN detail"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = ['MRN']\n",
    "mask = 0.0\n",
    "for i in target:\n",
    "    mask += get_region_mask(i)\n",
    "X = {}\n",
    "for id,neuron in enumerate(neuron_to_brainstem_information):\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']+neuron_to_brainstem_information[neuron]['cortex_subregion']\n",
    "    if region not in ['MOs','ACAd']:\n",
    "        continue\n",
    "    if region not in X:\n",
    "        X[region] = {'terminal':[]}\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            X[region]['terminal'].append(terminal.copy())\n",
    "            if neuron_terminals[neuron]['mirror_mark']:\n",
    "                X[region]['terminal'][-1][2] = 11400-X[region]['terminal'][-1][2]\n",
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "for i in target:\n",
    "    neuronvis.addRegion(i)\n",
    "for i in ['MOs']:\n",
    "    draw_points(neuronvis,X[i]['terminal'],[color_pool[0].tolist() for j in X[i]['terminal']])\n",
    "for i in ['ACAd']:\n",
    "    draw_points(neuronvis,X[i]['terminal'],[color_pool[3].tolist() for j in X[i]['terminal']])\n",
    "neuronvis.render.setPointSize(3)\n",
    "for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "    set_view(neuronvis,i)\n",
    "    neuronvis.render.savepng(midbrain_path+'MRN_terminal_'+i+'.png')\n",
    "neuronvis.render.closeWindow()\n",
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "for i in target:\n",
    "    neuronvis.addRegion(i)\n",
    "for i in ['MOs']:\n",
    "    draw_points(neuronvis,X[i]['terminal'],[color_pool[0].tolist() for j in X[i]['terminal']])\n",
    "neuronvis.render.setPointSize(3)\n",
    "for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "    set_view(neuronvis,i)\n",
    "    neuronvis.render.savepng(midbrain_path+'MRN_terminal_MOs_'+i+'.png')\n",
    "neuronvis.render.closeWindow()\n",
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "for i in target:\n",
    "    neuronvis.addRegion(i)\n",
    "for i in ['ACAd']:\n",
    "    draw_points(neuronvis,X[i]['terminal'],[color_pool[3].tolist() for j in X[i]['terminal']])\n",
    "neuronvis.render.setPointSize(3)\n",
    "for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "    set_view(neuronvis,i)\n",
    "    neuronvis.render.savepng(midbrain_path+'MRN_terminal_ACA_'+i+'.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### PAG detail"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "info = {}\n",
    "for i in neuron_to_brainstem_information:\n",
    "    d = neuron_to_brainstem_information[i]['soma_depth']\n",
    "    p = neuron_to_brainstem_information[i]['soma_flatten_position']\n",
    "    info[i] = [d,p]\n",
    "    info[i].append(neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][brainstem_region.index('PAG')])\n",
    "r = 200\n",
    "s = 125\n",
    "transition_color = np.array([[255,255,255],[161,238,204],[255,242,193],[246,49,141]])/255.0\n",
    "cmap1 = mcolors.LinearSegmentedColormap.from_list('cmap1', transition_color)\n",
    "kernel = generate_gaussian_kernel(r,s)\n",
    "tmp = _flatmap[...,0]*0.0\n",
    "soma_list = [[],[]]\n",
    "for i in info:\n",
    "    d,p = info[i][:2]\n",
    "    e = info[i][2]\n",
    "    fill_kernel(tmp,np.array(p[::-1])*2,e/neuron_to_brainstem_information[i]['relative_density'],r,kernel)\n",
    "    soma_list[0].append(np.array(p[::-1])*2)\n",
    "    soma_list[1].append(e)\n",
    "soma_list[1] = np.array(soma_list[1])**0.5\n",
    "soma_list[1] /= np.max(soma_list[1])\n",
    "#result = plot_transition_color(transition_color,tmp)\n",
    "result = np.where(_flatmap[...,0]==-1,0,tmp)\n",
    "for k,l in zip(soma_list[0],soma_list[1]):\n",
    "    cv.circle(result,[int(np.round(k[1])), int(np.round(k[0]))],int(np.round(l*10))+1,np.array([0.5,0.5,0.5]),-1)\n",
    "fig = plt.figure(figsize=(7,5))\n",
    "result /= np.max(result)\n",
    "plt.imshow(result,cmap=cmap1,vmin=0,vmax=1)\n",
    "plt.colorbar(extend='both')\n",
    "plt.imshow(flatmap,zorder=1)\n",
    "plt.axis('off')\n",
    "plt.xlim(flatten_map_r[0],flatten_map_r[1])\n",
    "plt.ylim(flatten_map_r[2],flatten_map_r[3])\n",
    "plt.title('PAG projection',fontsize=fontsize_title)\n",
    "plt.savefig(midbrain_path+'PAG_origion.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mask = get_region_mask('PAG')\n",
    "X = {}\n",
    "for id,neuron in enumerate(neuron_to_brainstem_information):\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']+neuron_to_brainstem_information[neuron]['cortex_subregion']\n",
    "    if region not in X:\n",
    "        X[region] = {'terminal':[]}\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            X[region]['terminal'].append(terminal.copy())\n",
    "            if neuron_terminals[neuron]['mirror_mark']:\n",
    "                X[region]['terminal'][-1][2] = 11400-X[region]['terminal'][-1][2]\n",
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "neuronvis.addRegion('PAG')\n",
    "for i in ['MOs','ACAd','ACAv']:\n",
    "    draw_points(neuronvis,X[i]['terminal'],[color_pool[0].tolist() for j in X[i]['terminal']])\n",
    "for i in ['RSPagl','RSPd','RSPv','VISam']:\n",
    "    draw_points(neuronvis,X[i]['terminal'],[color_pool[3].tolist() for j in X[i]['terminal']])\n",
    "for i in ['AUDv','TEa','ECT','PERI']:\n",
    "    draw_points(neuronvis,X[i]['terminal'],[color_pool[1].tolist() for j in X[i]['terminal']])\n",
    "for i in ['AId','AIv','AIp']:\n",
    "    draw_points(neuronvis,X[i]['terminal'],[color_pool[5].tolist() for j in X[i]['terminal']])\n",
    "neuronvis.render.setPointSize(1)\n",
    "for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "    set_view(neuronvis,i)\n",
    "    neuronvis.render.savepng(midbrain_path+'PAG_terminal_'+i+'.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mask = get_region_mask('PAG')\n",
    "X = {}\n",
    "for id,neuron in enumerate(neuron_to_brainstem_information):\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']+neuron_to_brainstem_information[neuron]['cortex_subregion']\n",
    "    if region not in X:\n",
    "        X[region] = {'terminal':[]}\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            X[region]['terminal'].append(terminal.copy())\n",
    "            if neuron_terminals[neuron]['mirror_mark']:\n",
    "                X[region]['terminal'][-1][2] = 11400-X[region]['terminal'][-1][2]\n",
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "neuronvis.addRegion('PAG')\n",
    "for i in ['MOs','ACAd','ACAv']:\n",
    "    draw_points(neuronvis,X[i]['terminal'],[color_pool[0].tolist() for j in X[i]['terminal']])\n",
    "for i in ['RSPagl','RSPd','RSPv','VISam']:\n",
    "    draw_points(neuronvis,X[i]['terminal'],[color_pool[3].tolist() for j in X[i]['terminal']])\n",
    "for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "    set_view(neuronvis,i)\n",
    "    neuronvis.render.savepng(midbrain_path+'PAG_terminal_VIS_'+i+'.png')\n",
    "neuronvis.render.closeWindow()\n",
    "neuronvis.clear(root=True)\n",
    "for i in ['AUDv','TEa','ECT','PERI']:\n",
    "    draw_points(neuronvis,X[i]['terminal'],[color_pool[1].tolist() for j in X[i]['terminal']])\n",
    "for i in ['AId','AIv','AIp']:\n",
    "    draw_points(neuronvis,X[i]['terminal'],[color_pool[5].tolist() for j in X[i]['terminal']])\n",
    "neuronvis.render.setPointSize(1)\n",
    "for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "    set_view(neuronvis,i)\n",
    "    neuronvis.render.savepng(midbrain_path+'PAG_terminal_AUD_'+i+'.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp = [['RSPagl','RSPd','RSPv','VISam'],['MOs','ACAd','ACAv'],['AUDv','TEa','ECT','PERI'],['AId','AIv','AIp']]\n",
    "x_labels = ['VIS','ACA','TEa','AI']\n",
    "tmp2 = {3,0,1,5}\n",
    "x = {}\n",
    "for i,j in zip(x_labels,tmp):\n",
    "    x[i] = []\n",
    "    for k in j:\n",
    "        x[i] += X[k]['terminal']\n",
    "fig = plt.figure(figsize=(25,5))\n",
    "ax = fig.add_subplot(1,3,1)\n",
    "tmp = [np.array(x[i])[:,0] for i in x_labels]\n",
    "parts = ax.violinplot(tmp,showmeans=False,showextrema=False,showmedians=False,vert=False)\n",
    "for pc,c in zip(parts['bodies'],tmp2):\n",
    "    pc.set_facecolor(color_pool[c])\n",
    "    pc.set_edgecolor('black')\n",
    "    pc.set_alpha(1)\n",
    "ax.scatter([np.median(i) for i in tmp],range(1,len(tmp)+1),color='k',s=10)\n",
    "ax.plot([np.median(i) for i in tmp],range(1,len(tmp)+1),color='k',linestyle='--')\n",
    "ax.invert_yaxis()\n",
    "ax.set_yticks([i+1 for i in range(len(x_labels))],[i+'\\n(n='+str(len(x[i]))+')' for i in x_labels],fontsize=fontsize_ticks)\n",
    "ax.set_ylabel('Modality',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "ax.set_xlabel('Distance from rostral (\\u00B5m)',fontsize=fontsize_label)\n",
    "ax.set_title('Terminal distribution in rostral-caudal axis',fontsize=fontsize_title)\n",
    "ax = fig.add_subplot(1,3,2)\n",
    "tmp = [np.array(x[i])[:,2] for i in x_labels]\n",
    "parts = ax.violinplot(tmp,showmeans=False,showextrema=False,showmedians=False,vert=False)\n",
    "for pc,c in zip(parts['bodies'],tmp2):\n",
    "    pc.set_facecolor(color_pool[c])\n",
    "    pc.set_edgecolor('black')\n",
    "    pc.set_alpha(1)\n",
    "ax.scatter([np.median(i) for i in tmp],range(1,len(tmp)+1),color='k',s=10)\n",
    "ax.plot([np.median(i) for i in tmp],range(1,len(tmp)+1),color='k',linestyle='--')\n",
    "ax.invert_yaxis()\n",
    "ax.set_yticks([i+1 for i in range(len(x_labels))],[i for i in x_labels],fontsize=fontsize_ticks)\n",
    "#ax.set_ylabel('Modality',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "ax.set_xlabel('Distance from lateral (\\u00B5m)',fontsize=fontsize_label)\n",
    "ax.set_title('Terminal distribution in lateral-medial axis',fontsize=fontsize_title)\n",
    "ax = fig.add_subplot(1,3,3)\n",
    "tmp = [np.array(x[i])[:,1] for i in x_labels]\n",
    "parts = ax.violinplot(tmp,showmeans=False,showextrema=False,showmedians=False)\n",
    "for pc,c in zip(parts['bodies'],tmp2):\n",
    "    pc.set_facecolor(color_pool[c])\n",
    "    pc.set_edgecolor('black')\n",
    "    pc.set_alpha(1)\n",
    "ax.scatter(range(1,len(tmp)+1),[np.median(i) for i in tmp],color='k',s=10)\n",
    "ax.plot(range(1,len(tmp)+1),[np.median(i) for i in tmp],color='k',linestyle='--')\n",
    "ax.invert_yaxis()\n",
    "ax.set_xticks([i+1 for i in range(len(x_labels))],[i for i in x_labels],fontsize=fontsize_ticks)\n",
    "ax.set_xlabel('Modality',fontsize=fontsize_label)\n",
    "ax.set_ylabel('Distance from dorsal (\\u00B5m)',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "ax.set_title('Terminal distribution in dorsal-ventral axis',fontsize=fontsize_title)\n",
    "plt.savefig(midbrain_path+'PAG_statistics.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## sensory system"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "midbrain_info = np.load(midbrain_path+'projection_axon_info.npy',allow_pickle=True).item()\n",
    "pons_info = np.load(pons_path+'projection_axon_info.npy',allow_pickle=True).item()\n",
    "medulla_info = np.load(medulla_path+'projection_axon_info.npy',allow_pickle=True).item()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear()\n",
    "f,d = draw_points,2\n",
    "for i,j in zip(['AUD','SSp','VIS','AI'],[2,1,3,4,5]):\n",
    "    f(neuronvis,midbrain_info[i]['axon'][d],set_color(midbrain_info[i]['axon'][d],color_pool[j]))\n",
    "    f(neuronvis,pons_info[i]['axon'][d],set_color(pons_info[i]['axon'][d],color_pool[j]))\n",
    "    f(neuronvis,medulla_info[i]['axon'][d],set_color(medulla_info[i]['axon'][d],color_pool[j]))\n",
    "neuronvis.render.setPointSize(1)\n",
    "for i in ['PB']:\n",
    "    neuronvis.addRegion(i)\n",
    "neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear()\n",
    "f,d = draw_points,2\n",
    "for i,j in zip(['MOs','ACA','RSP','ORB'],[1,4,4,5]):\n",
    "    f(neuronvis,midbrain_info[i]['axon'][d],set_color(midbrain_info[i]['axon'][d],color_pool[j]))\n",
    "    f(neuronvis,pons_info[i]['axon'][d],set_color(pons_info[i]['axon'][d],color_pool[j]))\n",
    "    f(neuronvis,medulla_info[i]['axon'][d],set_color(medulla_info[i]['axon'][d],color_pool[j]))\n",
    "neuronvis.render.setPointSize(1)\n",
    "for i in ['PAG']:\n",
    "    neuronvis.addRegion(i,[1,1,0.5])\n",
    "neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear()\n",
    "f,d = draw_lines,0\n",
    "for i,j in zip(['MOs','ACA','RSP','ORB'],[1,4,4,5]):\n",
    "    f(neuronvis,pons_info[i]['axon'][d],set_color(pons_info[i]['axon'][d],color_pool[j]))\n",
    "neuronvis.render.setPointSize(1)\n",
    "for i in ['P']:\n",
    "    neuronvis.addRegion(i,[1,1,0.5])\n",
    "neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Mono-/Multi-targeting and parallel control"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "coupling_path = data_path+'coupling/'\n",
    "create_dir(coupling_path)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## examples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "examples = ['221644055.swc','221531023.swc','220197303.swc']\n",
    "#examples = ['221644055.swc','221085026.swc','221478044.swc']\n",
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "for i,j in zip(examples,[1,0,3]):\n",
    "    neuronvis.clear()\n",
    "    neuron_tree = iondata.getNeuronTreeByID(i[:-7],i[-7:])\n",
    "    neuron_tree.somaRadius = 10\n",
    "    neuronvis.addNeuronTree(neuron_tree,axonColor=color_pool[j],somaColor=[0.5,0.5,0.5],dendriteColor=[0.5,0.5,0.5],mirrorToRight=True,isLine=False)\n",
    "    neuronvis.render.savepng(coupling_path+i[:-4]+'.png')\n",
    "    for k in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "        set_view(neuronvis,k)\n",
    "        neuronvis.render.savepng(coupling_path+i[:-4]+'_'+k+'.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in examples:\n",
    "    print(neuron_to_brainstem_information[i]['cortex_region'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = {}\n",
    "for i in ['MB','P','MY']:\n",
    "    X[i] = np.sum(get_neuron_axon_info(examples,terminal_or_length='terminal',if_combined=True,if_log=False,filter=brainstem_region_hierachy[i])[1],axis=-1)\n",
    "fig = plt.figure(figsize=(2,5))\n",
    "result = np.array(list(X.values())).T.astype(float)\n",
    "result /= np.linalg.norm(result,axis=-1).reshape(-1,1)\n",
    "plt.figure(figsize=(2,5))\n",
    "plt.matshow(result.T,cmap='BuGn',vmin=0,vmax=1,aspect='auto')\n",
    "plt.yticks(range(len(X)),X.keys(),fontsize=fontsize_ticks)\n",
    "plt.xticks([0,1,2],examples,fontsize=fontsize_ticks/1.25)\n",
    "plt.xlabel('Neurons',fontsize=fontsize_label)\n",
    "plt.ylabel('Traget',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.title('Feature vector',fontsize=fontsize_title)\n",
    "plt.colorbar()\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(coupling_path+'feature_vector.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp = np.array(list(X.values())).T\n",
    "tmp/np.sum(tmp,axis=-1).reshape(-1,1)*100"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## circos"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "target = brainstem_region_hierachy['MB']+brainstem_region_hierachy['P']+brainstem_region_hierachy['MY']+brainstem_region_hierachy['SC']\n",
    "X = get_neuron_axon_info(cortex_neuron_name,terminal_or_length='terminal',if_combined=True,if_log=False,filter=target)[1]\n",
    "target2 = target.copy()\n",
    "target2.pop(target2.index('SPVI'))\n",
    "target2.pop(target2.index('SPVC'))\n",
    "target2[target2.index('SPVO')] = 'SPV'\n",
    "X[:,target.index('SPVC')] = np.sum(X[:,target.index('SPVC'):target.index('SPVO')+3],axis=-1)\n",
    "X[:,target.index('SPVC')+1:-2] = X[:,target.index('SPVO')+1:]\n",
    "X = X[:,:-2]\n",
    "target = target2\n",
    "target2 = target.copy()\n",
    "target2.pop(target2.index('PRNc'))\n",
    "target2[target2.index('PRNr')] = 'PRN'\n",
    "X[:,target.index('PRNr')] = np.sum(X[:,target.index('PRNr'):target.index('PRNr')+2],axis=-1)\n",
    "X[:,target.index('PRNc'):-1] = X[:,target.index('PRNc')+1:]\n",
    "X = X[:,:-1]\n",
    "target = target2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import scipy.cluster.hierarchy as sch\n",
    "import radialtree as rt\n",
    "from scipy.stats import chi2_contingency\n",
    "hierarchy.set_link_color_palette(color_pool_hex)\n",
    "cluster_threshold = 7\n",
    "X_length = np.max(X,axis=-1)\n",
    "feature = X/np.where(X_length==0,1,X_length).reshape(-1,1)+1e-8\n",
    "Z = hierarchy.linkage(feature,'ward')\n",
    "clusters = hierarchy.fcluster(Z,t=cluster_threshold,criterion='distance')\n",
    "#n_samples = X.shape[0]\n",
    "#new_heights = np.arange(1,n_samples)\n",
    "Z_modified = Z.copy()\n",
    "Z_modified[:,2] = Z[:,2]**0.5\n",
    "cluster_object = hierarchy.dendrogram(Z_modified,distance_sort=True,orientation='top',color_threshold=cluster_threshold,labels=[i[:-4] for i in cortex_neuron_name],no_plot=True)\n",
    "cluster_object2 = hierarchy.dendrogram(Z,distance_sort=True,orientation='top',color_threshold=cluster_threshold,labels=[i[:-4] for i in cortex_neuron_name],no_plot=True)\n",
    "cluster_info = {}\n",
    "for i,j in zip(cortex_neuron_name,clusters):\n",
    "    if j not in cluster_info:\n",
    "        cluster_info[j] = []\n",
    "    cluster_info[j].append(i)\n",
    "for i in cluster_info:\n",
    "    save_scene(cluster_info[i],color_pool[i-1],coupling_path+f'cluster_{i}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### clustering panel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig,axes = plt.subplots(1,2, figsize=(10,5))\n",
    "axes[0].set_aspect(1)\n",
    "info = np.load(overall_path+'_info4.npy',allow_pickle=True).item()\n",
    "label_color = {i[:-4]:color_pool[j] for i,j in zip(cortex_neuron_name,clusters)}\n",
    "type_num = 10\n",
    "_cmp = cm.get_cmap(\"winter\",type_num)\n",
    "_cmp2 = cm.get_cmap(\"summer\",type_num)\n",
    "_cmp3 = cm.get_cmap(\"Reds\",type_num)\n",
    "sequence = [cortex_neuron_name[i] for i in cluster_object2['leaves']]\n",
    "density = np.array([info[i][1]**2 if i in info else 0 for i in sequence])\n",
    "volume = np.array([info[i][0]**0.3333 if i in info else 0 for i in sequence])\n",
    "BS_info = np.array([get_axonal_statics_by_regions(i,['MB','P','MY'],'terminalregion') for i in sequence])\n",
    "max_density = np.max(density)\n",
    "min_density = np.min(np.where(density==0,10000,density))\n",
    "max_volume = np.max(volume)\n",
    "min_volume = np.min(np.where(volume==0,10000,volume))\n",
    "d = (density-min_density)/(max_density-min_density)//(1/type_num)\n",
    "d = np.where(d<0,0,d).astype(int)\n",
    "v = (volume-min_volume)/(max_volume-min_volume)//(1/type_num)\n",
    "v = np.where(v<0,0,v).astype(int)\n",
    "MB = BS_info[:,0]\n",
    "P = BS_info[:,1]\n",
    "MY = BS_info[:,2]\n",
    "max_MB = np.max(MB)\n",
    "max_P = np.max(P)\n",
    "max_MY = np.max(MY)\n",
    "MB = MB/max_MB//(1/type_num)\n",
    "P = P/max_P//(1/type_num)\n",
    "MY = MY/max_MY//(1/type_num)\n",
    "colors_dict={\"MY\":_cmp3(type_num-MY),\"P\":_cmp3(type_num-P),\"MB\":_cmp3(type_num-MB),\"density\":_cmp(type_num-d),\"volume\":_cmp2(type_num-v)}\n",
    "colors_legends={\n",
    "    \"MY\":{\"colors\":_cmp3(np.linspace(0,1,type_num)),\"labels\": [f'{((type_num-i)/type_num*(max_MY-0)+0)}' for i in range(type_num)]},\n",
    "    \"P\":{\"colors\":_cmp3(np.linspace(0,1,type_num)),\"labels\": [f'{((type_num-i)/type_num*(max_P-0)+0)}' for i in range(type_num)]},\n",
    "    \"MB\":{\"colors\":_cmp3(np.linspace(0,1,type_num)),\"labels\": [f'{((type_num-i)/type_num*(max_MB-0)+0)}' for i in range(type_num)]},\n",
    "    \"density\":{\"colors\":_cmp(np.linspace(0,1,type_num)),\"labels\": [f'{((type_num-i)/type_num*(max_density-min_density)+min_density)**0.5}' for i in range(type_num)]},\n",
    "    \"volume\":{\"colors\":_cmp2(np.linspace(0,1,type_num)),\"labels\": [f'{((type_num-i)/type_num*(max_volume-min_volume)+min_volume)**3}' for i in range(type_num)]}}\n",
    "#rt.radialTreee(cluster_object2,ax=axes[0],colorlabels=colors_dict,addlabels=False,fontsize=1,pallete=None)\n",
    "rt.plot(cluster_object2,colorlabels=colors_dict,colorlabels_legend=colors_legends,addlabels=False,fontsize=1,pallete=None)\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(coupling_path+'overall_2.pdf',format='pdf',bbox_inches='tight',dpi=100)\n",
    "plt.show()\n",
    "ax_feature = axes[1]\n",
    "mask = np.max(X,axis=-1).reshape(-1,1)\n",
    "mask = np.sum(X>(mask*0.5),axis=0)\n",
    "mask = mask>5\n",
    "_target = []\n",
    "for i,j in zip(target2,mask):\n",
    "    if j:\n",
    "        _target.append(i)\n",
    "if 'unknow' in _target:\n",
    "    _target[_target.index('unknow')] = 'SPC'\n",
    "Y = X[:,mask]\n",
    "r = {}\n",
    "Sp = {}\n",
    "eee = {}\n",
    "for i,j in zip(Y,clusters):\n",
    "    if j not in r:\n",
    "        r[j] = []\n",
    "    r[j].append(i)\n",
    "for i in r:\n",
    "    tmp = np.mean(r[i],axis=0)\n",
    "    norm = np.sum(tmp)\n",
    "    tmp /= np.where(norm==0,1,norm)\n",
    "    Sp[i] = tmp\n",
    "ck = [np.argmax(Sp[i+1]) for i in range(len(Sp))]\n",
    "for index,i in enumerate(sorted(range(len(ck)),key=lambda k:ck[k])):\n",
    "    ax_feature.barh([-j for j in range(len(Sp[i+1]))],Sp[i+1].tolist(),left=index,color=color_pool[i])\n",
    "zz = np.zeros((len(Sp),len(Sp)))\n",
    "for i in range(len(Sp)):\n",
    "    for j in range(0,i):\n",
    "        op = np.array([Sp[i+1],Sp[j+1]])\n",
    "        op += np.where(np.sum(op,axis=0).reshape(1,-1)==0,1,np.sum(op,axis=0).reshape(1,-1)) # zeor expectation case\n",
    "        zz[i,j] = chi2_contingency(op)[1]\n",
    "        zz[j,i] = -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.plot(MB)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig,axes = plt.subplots(1,2, figsize=(10,5))\n",
    "axes[0].set_aspect(1)\n",
    "label_color = {i[:-4]:color_pool[j] for i,j in zip(cortex_neuron_name,clusters)}\n",
    "rt.radialTreee(cluster_object,ax=axes[0],addlabels=True,fontsize=1,pallete=None)\n",
    "ax_feature = axes[1]\n",
    "mask = np.max(X,axis=-1).reshape(-1,1)\n",
    "mask = np.sum(X>(mask*0.5),axis=0)\n",
    "mask = mask>5\n",
    "_target = []\n",
    "for i,j in zip(target2,mask):\n",
    "    if j:\n",
    "        _target.append(i)\n",
    "if 'unknow' in _target:\n",
    "    _target[_target.index('unknow')] = 'SPC'\n",
    "Y = X[:,mask]\n",
    "r = {}\n",
    "Sp = {}\n",
    "eee = {}\n",
    "for i,j in zip(Y,clusters):\n",
    "    if j not in r:\n",
    "        r[j] = []\n",
    "    r[j].append(i)\n",
    "for i in r:\n",
    "    tmp = np.mean(r[i],axis=0)\n",
    "    norm = np.sum(tmp)\n",
    "    tmp /= np.where(norm==0,1,norm)\n",
    "    Sp[i] = tmp\n",
    "ck = [np.argmax(Sp[i+1]) for i in range(len(Sp))]\n",
    "for index,i in enumerate(sorted(range(len(ck)),key=lambda k:ck[k])):\n",
    "    ax_feature.barh([-j for j in range(len(Sp[i+1]))],Sp[i+1].tolist(),left=index,color=color_pool[i])\n",
    "zz = np.zeros((len(Sp),len(Sp)))\n",
    "for i in range(len(Sp)):\n",
    "    for j in range(0,i):\n",
    "        op = np.array([Sp[i+1],Sp[j+1]])\n",
    "        op += np.where(np.sum(op,axis=0).reshape(1,-1)==0,1,np.sum(op,axis=0).reshape(1,-1)) # zeor expectation case\n",
    "        zz[i,j] = chi2_contingency(op)[1]\n",
    "        zz[j,i] = -1\n",
    "for i in range(len(Sp)-1):\n",
    "    ax_feature.axvline(i+1,linewidth=0.5,color='#AAAAAA')\n",
    "ax_feature.set_xlim(0,len(Sp))\n",
    "ax_feature.set_xticks([],[])\n",
    "ax_feature.set_yticks([-j for j in range(len(_target))],_target,fontsize=fontsize_ticks if len(Sp)<13 else fontsize_ticks*0.4)\n",
    "ax_feature.yaxis.tick_right()\n",
    "ax_feature.set_xlabel('Clusters',fontsize=fontsize_label)\n",
    "ax_feature.set_title('Projection feature',fontsize=fontsize_label)\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(coupling_path+'overall.pdf',format='pdf',bbox_inches='tight',dpi=100)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### source region distribution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig = plt.figure(figsize=(10,5))\n",
    "for i in range(len(cortex_region)):\n",
    "    plt.axvline(i,color='#CCCCCC',zorder=-1)\n",
    "y_ticks = []\n",
    "for index,i in enumerate(sorted(range(len(ck)),key=lambda k:ck[k])):\n",
    "    tmp = np.zeros((len(cortex_region)))\n",
    "    for neuron in cluster_info[i+1]:\n",
    "        tmp[cortex_region.index(neuron_to_brainstem_information[neuron]['cortex_region'])] += 1\n",
    "    x = np.array([j for j in range(len(cortex_region))])\n",
    "    y = x*0+len(ck)-1-index\n",
    "    plt.axhline(y[0],color='#CCCCCC',zorder=-1)\n",
    "    plt.scatter(x,y+0.5,s=tmp/2,color='#000000')\n",
    "    y_ticks.append(i+1)\n",
    "    plt.bar([len(cortex_region)+1+j for j in range(len(Sp[i+1]))],Sp[i+1].tolist(),bottom=y[0],color=color_pool[i])\n",
    "a = {}\n",
    "for i in [50,100,200]:\n",
    "    a[i] = plt.scatter([-100],[2],s=i/2,c='#000000')\n",
    "plt.xlim(-1,len(cortex_region)+len(_target)+1)\n",
    "plt.ylim(0,len(cluster_info))\n",
    "plt.legend(a.values(),['n='+str(i) for i in a.keys()],loc='upper right')\n",
    "plt.xticks(range(len(cortex_region)+len(_target)+1),cortex_region+['']+_target,rotation=90)\n",
    "plt.yticks([i+0.5 for i in range(len(ck))],y_ticks[::-1])\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(coupling_path+'overall_append.pdf',format='pdf',bbox_inches='tight',dpi=100)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "_target.index('PG')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Sp[4][_target.index('PG')]/Sp[4][_target.index('MRN')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Sp[4][_target.index('MRN')]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### parallel proof"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.metrics.pairwise import cosine_similarity\n",
    "Z = np.zeros((len(Sp),len(Sp)))\n",
    "for i in range(len(Sp)):\n",
    "    for j in range(len(Sp)):\n",
    "        Z[i,j] = cosine_similarity([Sp[i+1]/np.linalg.norm(Sp[i+1])],[Sp[j+1]/np.linalg.norm(Sp[j+1])])\n",
    "plt.matshow(Z,cmap='coolwarm',vmin=0,vmax=1)\n",
    "plt.xticks(range(len(Sp)),[i+1 for i in range(len(Sp))])\n",
    "plt.yticks(range(len(Sp)),[i+1 for i in range(len(Sp))])\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.colorbar()\n",
    "plt.savefig(coupling_path+'cosine_similarity.pdf',format='pdf',bbox_inches='tight',dpi=100)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.sum(Z<0.5)/(17*17-17)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Independent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "def statistic(x, y, axis):\n",
    "    return np.mean(x, axis=axis) - np.mean(y, axis=axis)\n",
    "Z = np.zeros((len(Sp),len(Sp)))\n",
    "for i in range(len(Sp)):\n",
    "    for j in range(len(Sp)):\n",
    "        a = np.array(random.choices(range(len(Sp[i+1])),weights=Sp[i+1],k=100000))\n",
    "        b = np.array(random.choices(range(len(Sp[j+1])),weights=Sp[j+1],k=100000))\n",
    "        Z[i,j] = stats.mannwhitneyu(a,b)[1]\n",
    "plt.matshow(Z,cmap='coolwarm',vmin=0,vmax=1)\n",
    "plt.xticks(range(len(Sp)),[i+1 for i in range(len(Sp))])\n",
    "plt.yticks(range(len(Sp)),[i+1 for i in range(len(Sp))])\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.colorbar()\n",
    "plt.savefig(coupling_path+'independet.pdf',format='pdf',bbox_inches='tight',dpi=100)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Z2 = []\n",
    "for i in range(len(Sp)):\n",
    "    for j in range(i+1,len(Sp)):\n",
    "        Z2.append(Z[i,j])\n",
    "Z2 = np.array(Z2)\n",
    "np.sum(Z2<0.01)/len(Z2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### coefficiency"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Z = np.zeros((len(Sp),len(Sp)))\n",
    "for i in range(len(Sp)):\n",
    "    for j in range(len(Sp)):\n",
    "        Z[i,j] = stats.pearsonr(Sp[i+1],Sp[j+1])[1]\n",
    "plt.matshow(Z,vmin=0,vmax=1,cmap='summer')\n",
    "plt.xticks(range(len(Sp)),[i+1 for i in range(len(Sp))])\n",
    "plt.yticks(range(len(Sp)),[i+1 for i in range(len(Sp))])\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.colorbar()\n",
    "plt.savefig(coupling_path+'coefficiency.pdf',format='pdf',bbox_inches='tight',dpi=100)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Z2 = []\n",
    "for i in range(len(Sp)):\n",
    "    for j in range(i+1,len(Sp)):\n",
    "        Z2.append(Z[i,j])\n",
    "Z2 = np.array(Z2)\n",
    "np.sum(Z2>0.01)/len(Z2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### region mono/multi ratio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "region_ratio = {i:[0,0] for i in cortex_region}\n",
    "for cluster in cluster_info:\n",
    "    if cluster==15:\n",
    "        continue\n",
    "    if cluster in [5,8,9]:\n",
    "        mark = 1\n",
    "    else:\n",
    "        mark = 0\n",
    "    for i in cluster_info[cluster]:\n",
    "        region = neuron_to_brainstem_information[i]['cortex_region']\n",
    "        region_ratio[region][mark] += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = [i[0]/np.sum(i) for i in region_ratio.values()]\n",
    "plt.bar(range(len(cortex_region)),x,width=0.7,color=np.array([239,65,53])/255)\n",
    "plt.axhline(np.mean(x),color='#AAAAAA')\n",
    "plt.xticks(range(len(cortex_region)),cortex_region,rotation=90)\n",
    "plt.rcParams['pdf.fonttype'] = 42\n",
    "plt.rcParams['ps.fonttype'] = 42\n",
    "plt.savefig(coupling_path+'mono_multi_ratio.pdf',format='pdf',bbox_inches='tight',dpi=100)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "d = {j:i for i,j in zip(cortex_region,x)}\n",
    "\n",
    "myKeys = list(d.keys())\n",
    "myKeys.sort()\n",
    "\n",
    "# Sorted Dictionary\n",
    "sd = {i: d[i] for i in myKeys}\n",
    "for i in sd:\n",
    "    print(i,sd[i])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### visualization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.render.setPointSize(1)\n",
    "bs = ['MB','P','MY']\n",
    "bs_mask = 0\n",
    "for i in bs:\n",
    "    bs_mask += get_region_mask(i)\n",
    "for cluster in cluster_info:\n",
    "    neuronvis.clear(root=False)\n",
    "    X = []\n",
    "    for neuron in cluster_info[cluster][:200]:\n",
    "        neuron_tree = iondata.getNeuronTreeByID(neuron[:-7],neuron[-7:])\n",
    "        neuron_tree.somaRadius = 10\n",
    "        neuronvis.addNeuronTree(neuron_tree,axonColor=color_pool[cluster-1],somaColor=[0.5,0.5,0.5],dendriteColor=[0.5,0.5,0.5],mirrorToRight=neuron_tree.root.z>5700,isLine=True,dendriteHide=True)\n",
    "        for terminal in neuron_terminals[neuron]['terminals']:\n",
    "            if if_inside_mask(bs_mask,np.array(terminal)/10) or terminal[0]>=13200:\n",
    "                X.append(terminal.copy())\n",
    "                if neuron_terminals[neuron]['mirror_mark']:\n",
    "                    X[-1][2] = 11400-X[-1][2]\n",
    "    for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "        set_view(neuronvis,i)\n",
    "        neuronvis.render.savepng(coupling_path+f'cluster_{cluster}_axon_{i}.png')\n",
    "    neuronvis.clear(root=False)\n",
    "    draw_points(neuronvis,X,[color_pool[cluster-1].tolist() for i in X])\n",
    "    for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "        set_view(neuronvis,i)\n",
    "        neuronvis.render.savepng(coupling_path+f'cluster_{cluster}_terminal_{i}.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### single neuron examples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.render.setPointSize(1)\n",
    "tmp = {2:['221072245.swc','APN'],1:['212073012.swc','SCs'],3:['220188047.swc','SCm'],6:['221476124.swc','IC'],\n",
    "       12:['220188037.swc','MRN'],7:['221225211.swc','PAG'],4:['221525006.swc','PG'],16:['221477055.swc','TRN'],\n",
    "       13:['220190017.swc','PRNr','PRNc'],14:['220195081.swc','PB'],17:['221085116.swc','PCG'],11:['220198432.swc','SPVO','SPVI','SPVC'],\n",
    "       10:['221072241.swc'],5:['221734016.swc','SCm','PG'],8:['220195115.swc','SCm','MRN','PAG'],9:['212075042.swc','MRN','IRN','PARN']}\n",
    "for a in tmp:\n",
    "    neuronvis.clear(root=False)\n",
    "    b = tmp[a]\n",
    "    neuron_tree = iondata.getNeuronTreeByID(b[0][:-7],b[0][-7:])\n",
    "    neuron_tree.somaRadius = 50\n",
    "    neuronvis.addNeuronTree(neuron_tree,axonColor=color_pool[a-1].tolist(),somaColor=[0.5,0.5,0.5],dendriteColor=[0.5,0.5,0.5],mirrorToRight=neuron_tree.root.z>5700,isLine=True,dendriteHide=True)\n",
    "    for i in b[1:]:\n",
    "        neuronvis.addRegion(i,[0.65,0.65,0.65])\n",
    "    for i in ['left']:\n",
    "        set_view(neuronvis,i)\n",
    "        neuronvis.render.savepng(coupling_path+f'cluster_{a}_{b[0][:-4]}_{i}.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### video"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "bs = ['MB','P','MY']\n",
    "bs_mask = 0\n",
    "for i in bs:\n",
    "    bs_mask += get_region_mask(i)\n",
    "for cluster in [13]:\n",
    "    neuronvis = nv.neuronVis(size=(1200,900))\n",
    "    neuronvis.render.setBackgroundColor((0.0,0.0,0.0,1.0))\n",
    "    neuronvis.render.setPointSize(1)\n",
    "    neuronvis.clear(root=False)\n",
    "    X = []\n",
    "    for neuron in cluster_info[cluster][:200]:\n",
    "        neuron_tree = iondata.getNeuronTreeByID(neuron[:-7],neuron[-7:])\n",
    "        neuron_tree.somaRadius = 10\n",
    "        neuronvis.addNeuronTree(neuron_tree,axonColor=color_pool[cluster-1],somaColor=[0.5,0.5,0.5],dendriteColor=[0.5,0.5,0.5],mirrorToRight=neuron_tree.root.z>5700,isLine=True,dendriteHide=True)\n",
    "        for terminal in neuron_terminals[neuron]['terminals']:\n",
    "            if if_inside_mask(bs_mask,np.array(terminal)/10) or terminal[0]>=13200:\n",
    "                X.append(terminal.copy())\n",
    "                if neuron_terminals[neuron]['mirror_mark']:\n",
    "                    X[-1][2] = 11400-X[-1][2]\n",
    "    for i in ['dorsal']:\n",
    "        set_view(neuronvis,i)\n",
    "        neuronvis.render.animation(f'cluster_{cluster}_axon')\n",
    "    neuronvis = nv.neuronVis(size=(1200,900))\n",
    "    neuronvis.render.setBackgroundColor((0.0,0.0,0.0,1.0))\n",
    "    neuronvis.render.setPointSize(1)\n",
    "    neuronvis.clear(root=False)\n",
    "    draw_points(neuronvis,X,[color_pool[cluster-1].tolist() for i in X])\n",
    "    for i in ['dorsal']:\n",
    "        set_view(neuronvis,i)\n",
    "        neuronvis.render.animation(f'cluster_{cluster}_terminal')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### soma panel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp = _flatmap*1\n",
    "for i in cluster_info:\n",
    "        if i==15:\n",
    "             continue\n",
    "        for j in cluster_info[i]:\n",
    "            l = neuron_to_brainstem_information[j]['soma_flatten_position']\n",
    "            if len(l)>0:\n",
    "                cv.circle(tmp,[int(l[0])*2, int(l[1])*2],12,np.array(color_pool[i-1])*255,-1)\n",
    "fig = plt.figure(figsize=(10,10))\n",
    "plt.imshow(tmp)\n",
    "plt.xlim(flatten_map_r[0],flatten_map_r[1])\n",
    "plt.ylim(flatten_map_r[2],flatten_map_r[3])\n",
    "plt.axis(\"equal\")\n",
    "plt.axis('off')\n",
    "plt.title('Cluster distribution',fontsize=fontsize_title)\n",
    "plt.savefig(coupling_path+'cluster_in_flatten_map.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp = _flatmap*1\n",
    "x = []\n",
    "y = []\n",
    "for i in cluster_info:\n",
    "        for j in cluster_info[i]:\n",
    "            l = neuron_to_brainstem_information[j]['soma_flatten_position']\n",
    "            if len(l)>0:\n",
    "                x.append(l+[neuron_to_brainstem_information[j]['soma_depth']])\n",
    "                y.append(color_pool[i-1])\n",
    "fig = plt.figure(figsize=(10,10))\n",
    "x = np.array(x)\n",
    "plt.scatter(x[:,0],x[:,2]*20,color=y,s=5)\n",
    "plt.axis(\"equal\")\n",
    "plt.axis('off')\n",
    "plt.title('Cluster depth',fontsize=fontsize_title)\n",
    "plt.savefig(coupling_path+'cluster_in_depth_map.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### mono and multi projection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "threshold = 0.5\n",
    "cluster_info_2 = {}\n",
    "for i in Sp:\n",
    "    cluster_info_2[i] = []\n",
    "    mask = Sp[i]>=np.max(Sp[i])*threshold\n",
    "    for j,k in enumerate(mask):\n",
    "        if k:\n",
    "            cluster_info_2[i].append(_target[j])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cluster_info_2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## group module version"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "module_neuron = {}\n",
    "for neuron in neuron_to_brainstem_information:\n",
    "    module = cortical_module_transform[neuron_to_brainstem_information[neuron]['cortex_region']]\n",
    "    if module in ['VIS','SS','AUD']:\n",
    "        module = 'Sensory'\n",
    "    elif module in ['MO','Cingulate']:\n",
    "        module = 'Dorsal-frontal'\n",
    "    else:\n",
    "        module = 'Ventral-frontal'#['PFC','Insula','TEa]\n",
    "    if module not in module_neuron:\n",
    "        module_neuron[module] = []\n",
    "    module_neuron[module].append(neuron)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from scipy.stats import chi2_contingency\n",
    "from scipy.spatial.distance import pdist, squareform\n",
    "def cluster_neuron(neurons,regions,X,save_path,circle_size=20,color_threshold=5):\n",
    "    X_length = np.linalg.norm(X,axis=-1)\n",
    "    #X_length = np.sum(X,axis=-1)\n",
    "    feature = X/np.where(X_length==0,1,X_length).reshape(-1,1)+1e-8\n",
    "    fig = plt.figure(figsize=(7,5.5))\n",
    "    ax_dendro = fig.add_axes([0.1,0.8,0.39,0.2])\n",
    "    a = pdist(feature,metric='cosine')\n",
    "    cortex_cluster_object = hierarchy.dendrogram(hierarchy.linkage(a,'ward'),distance_sort=True,orientation='top',ax=ax_dendro,color_threshold=color_threshold)\n",
    "    ax_dendro.set_xticks([],[])\n",
    "    ax_dendro.set_yticks([],[])\n",
    "    ax_dendro.set_title('Clustering',fontsize=fontsize_label)\n",
    "    cluster_object_parse = [[],[]]\n",
    "    neuron_to_cluster = {}\n",
    "    for i,j in zip(cortex_cluster_object['leaves'],cortex_cluster_object['leaves_color_list']):\n",
    "        neuron = neurons[i]\n",
    "        projection_matrix = feature[i]\n",
    "        try:\n",
    "            cluster_object_parse[0].append(color_pool_hex.index(j))\n",
    "            cluster_object_parse[1].append(projection_matrix)\n",
    "            neuron_to_cluster[neuron] = color_pool_hex.index(j)\n",
    "        except:\n",
    "            cluster_object_parse[0].append(-1)\n",
    "            cluster_object_parse[1].append(projection_matrix)\n",
    "            neuron_to_cluster[neuron] = -1\n",
    "    cluster_object_parse[1] = np.array(cluster_object_parse[1])\n",
    "    ax_matrix = fig.add_axes([0.1,0.0,0.39,0.8])\n",
    "    ax_matrix.matshow(cluster_object_parse[1].T,cmap='BuGn',aspect='auto')\n",
    "    _ = ax_matrix.set_xticks([],[])\n",
    "    ax_matrix.set_xlabel('neurons = '+str(len(neurons)),fontsize=fontsize_label)\n",
    "    cluster_object_parse_visualization_yticks = [[],[]]\n",
    "    cluster_object_parse_visualization_yticks[0] = [i for i in range(len(regions))]\n",
    "    cluster_object_parse_visualization_yticks[1] = [i if i!='unknow' else 'Spine' for i in regions]\n",
    "    _ = ax_matrix.set_yticks(cluster_object_parse_visualization_yticks[0],cluster_object_parse_visualization_yticks[1],fontsize=fontsize_ticks)\n",
    "    ax_matrix.set_ylabel('Traget',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "    ax_soma = fig.add_axes([0.5,0.5,0.4,0.5])\n",
    "    cluster_info = {}\n",
    "    for i,j in enumerate(neurons):\n",
    "        _cluster = neuron_to_cluster[j]\n",
    "        if _cluster==-1:\n",
    "            print(j)\n",
    "            continue\n",
    "        if _cluster not in cluster_info:\n",
    "            cluster_info[_cluster] = []\n",
    "        cluster_info[_cluster].append([j,neuron_to_brainstem_information[j]['soma_flatten_position']])\n",
    "    flatmap_tmp = _flatmap*1\n",
    "    for i in cluster_info.keys():\n",
    "        tmp3 = []\n",
    "        for j in cluster_info[i]:\n",
    "            if np.sum(abs(np.array(j[1])))!=0:\n",
    "                cv.circle(flatmap_tmp,[int(j[1][0])*2, int(j[1][1])*2],circle_size,color_pool[i]*255,-1)\n",
    "            tmp3.append(j[0])\n",
    "        save_scene(tmp3,color_pool[i],save_path+'_cluster_'+str(i+1))\n",
    "    ax_soma.imshow(flatmap_tmp)\n",
    "    ax_soma.imshow(flatmap,zorder=1)\n",
    "    ax_soma.set_xlim(flatten_map_r[0],flatten_map_r[1])\n",
    "    ax_soma.set_ylim(flatten_map_r[2],flatten_map_r[3])\n",
    "    ax_soma.set_axis_off()\n",
    "    ax_soma.set_title('Soma distribution',fontsize=fontsize_label)\n",
    "    cluster_info_transform = {}\n",
    "    for i in cluster_info:\n",
    "        for j in cluster_info[i]:\n",
    "            cluster_info_transform[j[0]] = i\n",
    "    ax_feature = fig.add_axes([0.5,0.0,0.4,0.45])\n",
    "    r = {}\n",
    "    Sp = {}\n",
    "    eee = {}\n",
    "    for i in cluster_info.keys():\n",
    "        eee[i] = []\n",
    "        r[i] = []\n",
    "        for j in cluster_info[i]:\n",
    "            r[i].append(X[neurons.index(j[0])])\n",
    "        tmp = np.mean(r[i],axis=0)\n",
    "        eee[i] = np.mean(eee[i])/np.where(np.max(tmp)==0,1,np.max(tmp))\n",
    "        tmp /= np.where(np.linalg.norm(tmp)==0,1,np.linalg.norm(tmp)*1.1)\n",
    "        Sp[i] = tmp\n",
    "    _cls_list = sorted(list(cluster_info.keys()))\n",
    "    ck = [np.argmax(Sp[i]) for i in _cls_list]\n",
    "    for index,i in enumerate(sorted(range(len(ck)),key=lambda k:ck[k])):\n",
    "        ax_feature.barh([-j for j in range(len(Sp[_cls_list[i]]))],Sp[_cls_list[i]].tolist(),left=index,color=color_pool[_cls_list[i]])\n",
    "    zz = np.zeros((len(_cls_list),len(_cls_list)))\n",
    "    for i in range(len(_cls_list)):\n",
    "        for j in range(-1,i):\n",
    "            op = np.array([Sp[_cls_list[i]],Sp[_cls_list[j]]])\n",
    "            op += np.where(np.sum(op,axis=0).reshape(1,-1)==0,1,np.sum(op,axis=0).reshape(1,-1)) # zeor expectation case\n",
    "            zz[i,j] = chi2_contingency(op)[1]\n",
    "            zz[j,i] = -1\n",
    "    for i in range(len(cluster_info)-1):\n",
    "        ax_feature.axvline(i+1,linewidth=0.5,color='#AAAAAA')\n",
    "    ax_feature.set_xlim(0,len(cluster_info))\n",
    "    ax_feature.set_xticks([],[])\n",
    "    ax_feature.set_yticks([-j for j in range(len(tmp))],cluster_object_parse_visualization_yticks[1],fontsize=fontsize_ticks if len(cluster_info)<13 else fontsize_ticks*0.4)\n",
    "    ax_feature.yaxis.tick_right()\n",
    "    ax_feature.set_xlabel('Clusters',fontsize=fontsize_label)\n",
    "    ax_feature.set_title('Projection feature',fontsize=fontsize_label)\n",
    "    plt.rcParams['pdf.fonttype'] = 42\n",
    "    plt.rcParams['ps.fonttype'] = 42\n",
    "    plt.savefig(save_path+'_overall.pdf',format='pdf',bbox_inches='tight',dpi=100)\n",
    "    plt.show()\n",
    "    return cluster_info,zz,Sp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "g = []\n",
    "threshold = 0.5\n",
    "target = brainstem_region_hierachy['MB']+brainstem_region_hierachy['P']+brainstem_region_hierachy['MY']+brainstem_region_hierachy['SC']\n",
    "target2 = target.copy()\n",
    "target2.pop(target2.index('SPVI'))\n",
    "target2.pop(target2.index('SPVC'))\n",
    "target2[target2.index('SPVO')] = 'SPV'\n",
    "E = {}\n",
    "c_e = {'Sensory':2,'Dorsal-frontal':2,'Ventral-frontal':2}\n",
    "#c_e = {'Sensory':4,'MO':4,'Cingulate':4,'Insula':4,'PFC':3}\n",
    "for module in c_e:\n",
    "    source = module_neuron[module]\n",
    "    X = get_neuron_axon_info(source,terminal_or_length='terminal',if_combined=True,if_log=False,filter=target)[1]\n",
    "    X[:,target.index('SPVC')] = np.sum(X[:,target.index('SPVC'):target.index('SPVO')+3],axis=-1)\n",
    "    X[:,target.index('SPVC')+1:-2] = X[:,target.index('SPVO')+1:]\n",
    "    X = X[:,:-2]\n",
    "    mask = np.max(X,axis=-1).reshape(-1,1)\n",
    "    mask = np.sum(X>(mask*threshold),axis=0)\n",
    "    mask = mask>5\n",
    "    _target = []\n",
    "    for i,j in zip(target2,mask):\n",
    "        if j:\n",
    "            _target.append(i)\n",
    "    #Y = np.sum(X[:,~mask],axis=-1)\n",
    "    X = X[:,mask]\n",
    "    E[module],zz,debug = cluster_neuron(source,_target,X,save_path=coupling_path+module,circle_size=15,color_threshold=c_e[module])\n",
    "    fig = plt.figure(figsize=(5,5))\n",
    "    plt.matshow(zz>0.05,vmin=0,vmax=1,cmap='GnBu')\n",
    "    plt.title('Dependency',fontsize=fontsize_title)\n",
    "    plt.savefig(coupling_path+module+'_dependency.pdf',format='pdf',bbox_inches='tight')\n",
    "    plt.show()\n",
    "    for i in debug:\n",
    "        _max = np.max(debug[i])\n",
    "        c = []\n",
    "        for j,k in enumerate(debug[i]):\n",
    "            if k>_max*0.5:\n",
    "                c.append(_target[j])\n",
    "        print(i,c)\n",
    "        g.append([module,i+1,c])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "5/13,6/16,2/11"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import networkx as nx\n",
    "plt.figure(figsize=(10,5))\n",
    "_color = {'Sensory':3,'Dorsal-frontal':0,'Ventral-frontal':1}\n",
    "tmp = {'node':[],'node_color':{},'node_cluster':{},'edge':{},'edge_color':{}}\n",
    "for i in g:\n",
    "    for j_index,j in enumerate(i[2]):\n",
    "        if j=='unknow':\n",
    "            j = 'SPC'\n",
    "        if j not in tmp['node']:\n",
    "            tmp['node'].append(j)\n",
    "            tmp['node_color'][j] = [np.array(color_pool[_color[i[0]]]),1]\n",
    "            tmp['node_cluster'][j] = {i:0 for i in _color}\n",
    "            tmp['node_cluster'][j][i[0]] += 1\n",
    "        else:\n",
    "            tmp['node_color'][j][0] += np.array(color_pool[_color[i[0]]])\n",
    "            tmp['node_color'][j][1] += 1\n",
    "            tmp['node_cluster'][j][i[0]] += 1\n",
    "        for k in i[2][j_index+1:]:\n",
    "            c = j+'#'+k\n",
    "            if c not in tmp['edge']:\n",
    "                tmp['edge'][c] = 1\n",
    "                tmp['edge_color'][c] = [np.array(color_pool[_color[i[0]]]),1]\n",
    "            else:\n",
    "                tmp['edge'][c] += 1\n",
    "                tmp['edge_color'][c][0] += np.array(color_pool[_color[i[0]]])\n",
    "                tmp['edge_color'][c][1] += 1\n",
    "node_color = [rgb_to_hex(tmp['node_color'][i][0]/tmp['node_color'][i][1]*255) for i in tmp['node']]\n",
    "edge_color = [rgb_to_hex(tmp['edge_color'][i][0]/tmp['edge_color'][i][1]*255) for i in tmp['edge']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "G = nx.Graph()\n",
    "G.add_nodes_from(tmp['node'])\n",
    "G.add_weighted_edges_from([(i.split('#'))+[tmp['edge'][i]] for i in tmp['edge']])\n",
    "pos = nx.spring_layout( \n",
    "    G, \n",
    "    k=17.5,\n",
    "    iterations=2000,\n",
    "    seed=0\n",
    ")\n",
    "nx.draw(G, pos, \n",
    "        edgelist=[(i.split('#'))+[tmp['edge'][i]] for i in tmp['edge']], \n",
    "        edge_color=edge_color,\n",
    "        width=[tmp['edge'][i] for i in tmp['edge']], \n",
    "        with_labels=True,\n",
    "        node_color=node_color, \n",
    "        node_size=150,) \n",
    "plt.axis('off')\n",
    "plt.savefig(coupling_path+'brain_region_relationship.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i,j in zip(tmp['node'],tmp['node_cluster']):\n",
    "    print(i,tmp['node_cluster'][i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = []\n",
    "Y = []\n",
    "for theta in range(90,451,5):\n",
    "    if theta<210:\n",
    "        a = color_pool[3]\n",
    "        b = color_pool[1]\n",
    "        c = 1-(theta-90)/120\n",
    "    elif theta<330:\n",
    "        a = color_pool[1]\n",
    "        b = color_pool[0]\n",
    "        c = 1-(theta-210)/120\n",
    "    else:\n",
    "        a = color_pool[0]\n",
    "        b = color_pool[3]\n",
    "        c = 1-(theta-330)/120\n",
    "    X.append(theta)\n",
    "    Y.append(c*a+(1-c)*b)\n",
    "X = np.array(X)/360*2*np.pi\n",
    "_ = plt.pie(X*0+1,colors=Y,startangle=90)\n",
    "plt.axis('off')\n",
    "plt.savefig(coupling_path+'brain_region_relationship_legend.pdf',format='pdf',bbox_inches='tight')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### pie chart"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = {'Sensory':[6,12,13,11,2],'Dorsal-frontal':[15,16,3,8,15,11,13,4],'Ventral-frontal':[11,4]}\n",
    "for module in s:\n",
    "    m = 0\n",
    "    n = 0\n",
    "    for i in range(s[module][0]):\n",
    "        l = len(Scene.scene2List(coupling_path+module+'_cluster_'+str(i+1)+'.nv'))\n",
    "        m += l\n",
    "        if i+1 in s[module][1:]:\n",
    "            n += l\n",
    "    print(module,n,m,np.round(n/m*100))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### visualization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.render.setPointSize(3)\n",
    "for module in c_e:\n",
    "    for cluster in E[module]:\n",
    "        neuronvis.clear(root=False)\n",
    "        X = []\n",
    "        for neuron in E[module][cluster]:\n",
    "            neuron_tree = iondata.getNeuronTreeByID(neuron[0][:-7],neuron[0][-7:])\n",
    "            neuron_tree.somaRadius = 10\n",
    "            neuronvis.addNeuronTree(neuron_tree,axonColor=color_pool[cluster],somaColor=[0.5,0.5,0.5],dendriteColor=[0.5,0.5,0.5],mirrorToRight=True,isLine=True,dendriteHide=True)\n",
    "            for terminal in neuron_terminals[neuron[0]]['terminals']:\n",
    "                X.append(terminal.copy())\n",
    "                if neuron_terminals[neuron[0]]['mirror_mark']:\n",
    "                    X[-1][2] = 11400-X[-1][2]\n",
    "        for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "            set_view(neuronvis,i)\n",
    "            neuronvis.render.savepng(coupling_path+module+'_'+str(cluster)+'_axon_'+i+'.png')\n",
    "        neuronvis.clear(root=False)\n",
    "        draw_points(neuronvis,X,[color_pool[cluster].tolist() for i in X])\n",
    "        for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "            set_view(neuronvis,i)\n",
    "            neuronvis.render.savepng(coupling_path+module+'_'+str(cluster)+'_terminal_'+i+'.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.render.setPointSize(3)\n",
    "neuronvis.addRegion('MB')\n",
    "neuronvis.addRegion('P')\n",
    "neuronvis.addRegion('MY')\n",
    "set_view(neuronvis,'right')\n",
    "neuronvis.render.savepng(coupling_path+'test.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## module version"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "module_neuron = {}\n",
    "for neuron in neuron_to_brainstem_information:\n",
    "    module = cortical_module_transform[neuron_to_brainstem_information[neuron]['cortex_region']]\n",
    "    if module not in module_neuron:\n",
    "        module_neuron[module] = []\n",
    "    module_neuron[module].append(neuron)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from scipy.stats import spearmanr\n",
    "def cluster_neuron(neurons,regions,X,save_path,circle_size=20):\n",
    "    X_length = np.linalg.norm(X,axis=-1)\n",
    "    feature = X/np.where(X_length==0,1,X_length).reshape(-1,1)\n",
    "    fig = plt.figure(figsize=(7,5.5))\n",
    "    ax_dendro = fig.add_axes([0.1,0.8,0.39,0.2])\n",
    "    cortex_cluster_object = hierarchy.dendrogram(hierarchy.linkage(feature,'ward'),distance_sort=True,orientation='top',ax=ax_dendro)\n",
    "    ax_dendro.set_xticks([],[])\n",
    "    ax_dendro.set_yticks([],[])\n",
    "    ax_dendro.set_title('Clustering',fontsize=fontsize_label)\n",
    "    cluster_object_parse = [[],[]]\n",
    "    neuron_to_cluster = {}\n",
    "    for i,j in zip(cortex_cluster_object['leaves'],cortex_cluster_object['leaves_color_list']):\n",
    "        neuron = neurons[i]\n",
    "        projection_matrix = feature[i]\n",
    "        cluster_object_parse[0].append(color_pool_hex.index(j))\n",
    "        cluster_object_parse[1].append(projection_matrix)\n",
    "        neuron_to_cluster[neuron] = color_pool_hex.index(j)\n",
    "    cluster_object_parse[1] = np.array(cluster_object_parse[1])\n",
    "    ax_matrix = fig.add_axes([0.1,0.0,0.39,0.8])\n",
    "    ax_matrix.matshow(cluster_object_parse[1].T,cmap='BuGn')\n",
    "    _ = ax_matrix.set_xticks([],[])\n",
    "    ax_matrix.set_xlabel('neurons = '+str(len(neurons)),fontsize=fontsize_label)\n",
    "    cluster_object_parse_visualization_yticks = [[],[]]\n",
    "    cluster_object_parse_visualization_yticks[0] = [i for i in range(len(regions))]\n",
    "    cluster_object_parse_visualization_yticks[1] = [i if i!='unknow' else 'Spine' for i in regions]\n",
    "    _ = ax_matrix.set_yticks(cluster_object_parse_visualization_yticks[0],cluster_object_parse_visualization_yticks[1],fontsize=fontsize_ticks)\n",
    "    ax_matrix.set_ylabel('Traget',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "    ax_soma = fig.add_axes([0.5,0.5,0.4,0.5])\n",
    "    cluster_info = {}\n",
    "    for i,j in enumerate(neurons):\n",
    "        _cluster = color_pool_hex.index(cortex_cluster_object['leaves_color_list'][cortex_cluster_object['leaves'].index(i)])\n",
    "        if _cluster not in cluster_info:\n",
    "            cluster_info[_cluster] = []\n",
    "        cluster_info[_cluster].append([j,neuron_to_brainstem_information[j]['soma_flatten_position']])\n",
    "    flatmap_tmp = _flatmap*1\n",
    "    for i in range(len(list(cluster_info.keys()))):\n",
    "        tmp3 = []\n",
    "        for j in cluster_info[i]:\n",
    "            if np.sum(abs(np.array(j[1])))!=0:\n",
    "                cv.circle(flatmap_tmp,[int(j[1][0])*2, int(j[1][1])*2],circle_size,color_pool[i]*255,-1)\n",
    "            tmp3.append(j[0])\n",
    "        save_scene(tmp3,color_pool[i],save_path+'_cluster_'+str(i+1))\n",
    "    ax_soma.imshow(flatmap_tmp)\n",
    "    ax_soma.imshow(flatmap,zorder=1)\n",
    "    ax_soma.set_xlim(flatten_map_r[0],flatten_map_r[1])\n",
    "    ax_soma.set_ylim(flatten_map_r[2],flatten_map_r[3])\n",
    "    ax_soma.set_axis_off()\n",
    "    ax_soma.set_title('Soma  distribution',fontsize=fontsize_label)\n",
    "    cluster_info_transform = {}\n",
    "    for i in cluster_info:\n",
    "        for j in cluster_info[i]:\n",
    "            cluster_info_transform[j[0]] = i\n",
    "    ax_feature = fig.add_axes([0.5,0.0,0.4,0.45])\n",
    "    r = {}\n",
    "    xs = np.linspace(0,len(regions),500)\n",
    "    Sp = {}\n",
    "    for index,i in enumerate(range(len(cluster_info))):\n",
    "        r[i] = []\n",
    "        for j in cluster_info[i]:\n",
    "            r[i].append(X[neurons.index(j[0])])\n",
    "        tmp = np.mean(r[i],axis=0)\n",
    "        tmp /= np.sum(tmp)\n",
    "        Sp[i] = tmp\n",
    "        ax_feature.barh([-j for j in range(len(tmp))],tmp,left=index,color=color_pool[i])\n",
    "    for i in range(len(cluster_info)):\n",
    "        for j in range(i+1,len(cluster_info)):\n",
    "            print(str(i)+'_'+str(j),spearmanr(Sp[i],Sp[j])[1])\n",
    "    for i in range(len(cluster_info)-1):\n",
    "        ax_feature.axvline(i+1,linewidth=0.5,color='#AAAAAA')\n",
    "    ax_feature.set_xlim(0,len(cluster_info))\n",
    "    ax_feature.set_xticks([],[])\n",
    "    ax_feature.set_yticks([-j for j in range(len(tmp))],cluster_object_parse_visualization_yticks[1],fontsize=fontsize_ticks if len(cluster_info)<13 else fontsize_ticks*0.4)\n",
    "    ax_feature.yaxis.tick_right()\n",
    "    ax_feature.set_xlabel('Clusters',fontsize=fontsize_label)\n",
    "    ax_feature.set_title('Projection feature',fontsize=fontsize_label)\n",
    "    plt.savefig(save_path+'_overall.pdf',format='pdf',bbox_inches='tight')\n",
    "    plt.show()\n",
    "    return cluster_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "threshold = 0.1\n",
    "target = brainstem_region_hierachy['MB']+brainstem_region_hierachy['P']+brainstem_region_hierachy['MY']+brainstem_region_hierachy['SC']\n",
    "E = {}\n",
    "for module in cortical_module:\n",
    "    source = module_neuron[module]\n",
    "    X = get_neuron_axon_info(source,terminal_or_length='terminal',if_combined=False,if_log=False,filter=target)[1]\n",
    "    mask = np.sum(X,axis=0)\n",
    "    mask = mask>np.max(mask)*threshold\n",
    "    _target = []\n",
    "    for i,j in zip(target+[k+'_c' for k in target],mask):\n",
    "        if j:\n",
    "            _target.append(i)\n",
    "    X = X[:,mask]\n",
    "    E[module] = cluster_neuron(source,_target,X,save_path=coupling_path+module,circle_size=15)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuronvis = nv.neuronVis(size=(1200,900))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.render.setPointSize(3)\n",
    "for module in cortical_module:\n",
    "    for cluster in E[module]:\n",
    "        neuronvis.clear(root=False)\n",
    "        X = []\n",
    "        for neuron in E[module][cluster]:\n",
    "            neuron_tree = iondata.getNeuronTreeByID(neuron[0][:-7],neuron[0][-7:])\n",
    "            neuron_tree.somaRadius = 10\n",
    "            neuronvis.addNeuronTree(neuron_tree,axonColor=color_pool[cluster],somaColor=[0.5,0.5,0.5],dendriteColor=[0.5,0.5,0.5],mirrorToRight=True,isLine=True,dendriteHide=True)\n",
    "            for terminal in neuron_terminals[neuron[0]]['terminals']:\n",
    "                X.append(terminal.copy())\n",
    "                if neuron_terminals[neuron[0]]['mirror_mark']:\n",
    "                    X[-1][2] = 11400-X[-1][2]\n",
    "        for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "            set_view(neuronvis,i)\n",
    "            neuronvis.render.savepng(coupling_path+module+'_'+str(cluster)+'_axon_'+i+'.png')\n",
    "        neuronvis.clear(root=False)\n",
    "        draw_points(neuronvis,X,[color_pool[cluster].tolist() for i in X])\n",
    "        for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "            set_view(neuronvis,i)\n",
    "            neuronvis.render.savepng(coupling_path+module+'_'+str(cluster)+'_terminal_'+i+'.png')\n",
    "neuronvis.render.closeWindow()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# system"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "system_path = data_path+'system/'\n",
    "create_dir(system_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "neuron_color_mapping = []\n",
    "tmp = []\n",
    "for i in cortex_neuron_name:\n",
    "    if len(neuron_to_brainstem_information[i]['soma_flatten_position'])==0:\n",
    "        continue\n",
    "    tmp.append(i)\n",
    "    neuron_color_mapping.append(neuron_to_brainstem_information[i]['soma_flatten_position'])\n",
    "neuron_color_mapping = position_color_mapping(np.array(neuron_color_mapping),2)\n",
    "_flatmap = flatmap[...,:3]/255.0\n",
    "a,b = get_value_range(1-flatmap[...,0]/255)\n",
    "for i,j in zip(tmp,neuron_color_mapping):\n",
    "        l = neuron_to_brainstem_information[i]['soma_flatten_position']\n",
    "        cv.circle(_flatmap,[int(l[0])*2, int(l[1])*2],8,j,-1)\n",
    "fig = plt.figure(figsize=(10,10))\n",
    "plt.imshow(_flatmap)\n",
    "plt.xlim(flatten_map_r[0],flatten_map_r[1])\n",
    "plt.ylim(flatten_map_r[2],flatten_map_r[3])\n",
    "plt.axis('off')\n",
    "plt.title('Neuron color mapping',fontsize=fontsize_title)\n",
    "plt.savefig(system_path+'soma_color_coding.pdf',format='pdf',bbox_inches='tight',dpi=300)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## striatum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mask = 0.0\n",
    "target = ['STR']\n",
    "for i in target:\n",
    "    mask += get_region_mask(i)\n",
    "X = {'terminal':[],'color':[]}\n",
    "for neuron,neuron_color in zip(cortex_neuron_name,neuron_color_mapping):\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            X['terminal'].append(terminal.copy())\n",
    "            if neuron_terminals[neuron]['mirror_mark']:\n",
    "                X['terminal'][-1][2] = 11400-X['terminal'][-1][2]\n",
    "            X['color'].append(neuron_color.tolist())\n",
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "draw_points(neuronvis,X['terminal'],X['color'])\n",
    "for i in target:\n",
    "    neuronvis.addRegion(i)\n",
    "neuronvis.render.setPointSize(1)\n",
    "for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "    set_view(neuronvis,i)\n",
    "    neuronvis.render.savepng(system_path+'STR_'+i+'.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## pallidum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mask = 0.0\n",
    "target = ['PAL']\n",
    "for i in target:\n",
    "    mask += get_region_mask(i)\n",
    "X = {'terminal':[],'color':[]}\n",
    "for neuron,neuron_color in zip(cortex_neuron_name,neuron_color_mapping):\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            X['terminal'].append(terminal.copy())\n",
    "            if neuron_terminals[neuron]['mirror_mark']:\n",
    "                X['terminal'][-1][2] = 11400-X['terminal'][-1][2]\n",
    "            X['color'].append(neuron_color.tolist())\n",
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "draw_points(neuronvis,X['terminal'],X['color'])\n",
    "for i in target:\n",
    "    neuronvis.addRegion(i)\n",
    "neuronvis.render.setPointSize(2)\n",
    "for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "    set_view(neuronvis,i)\n",
    "    neuronvis.render.savepng(system_path+'PAL_'+i+'.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## HY and TH"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mask = 0.0\n",
    "target = ['HY','TH']\n",
    "for i in target:\n",
    "    mask += get_region_mask(i)\n",
    "X = {'terminal':[],'color':[]}\n",
    "for neuron,neuron_color in zip(cortex_neuron_name,neuron_color_mapping):\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            X['terminal'].append(terminal.copy())\n",
    "            if neuron_terminals[neuron]['mirror_mark']:\n",
    "                X['terminal'][-1][2] = 11400-X['terminal'][-1][2]\n",
    "            X['color'].append(neuron_color.tolist())\n",
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "draw_points(neuronvis,X['terminal'],X['color'])\n",
    "for i in target:\n",
    "    neuronvis.addRegion(i)\n",
    "neuronvis.render.setPointSize(2)\n",
    "for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "    set_view(neuronvis,i)\n",
    "    neuronvis.render.savepng(system_path+'HY_TH_'+i+'.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## sensory system"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mask = 0.0\n",
    "target = ['PRT','SCs','SCm','IC','NLL','PB','PG','TRN','PSV','SPVO','SPVI','SPVC','NTS','DCN','ECU','AP','CN','VNC']\n",
    "for i in target:\n",
    "    mask += get_region_mask(i)\n",
    "X = {'terminal':[],'color':[]}\n",
    "for neuron,neuron_color in zip(cortex_neuron_name,neuron_color_mapping):\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            X['terminal'].append(terminal.copy())\n",
    "            if neuron_terminals[neuron]['mirror_mark']:\n",
    "                X['terminal'][-1][2] = 11400-X['terminal'][-1][2]\n",
    "            X['color'].append(neuron_color.tolist())\n",
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "draw_points(neuronvis,X['terminal'],X['color'])\n",
    "for i in target:\n",
    "    neuronvis.addRegion(i)\n",
    "neuronvis.render.setPointSize(2)\n",
    "for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "    set_view(neuronvis,i)\n",
    "    neuronvis.render.savepng(system_path+'sensory_'+i+'.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## reticular system"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mask = 0.0\n",
    "target = ['MRN','PRNr','PRNc','GRN','IRN','PARN','MDRN','PGRN','LRN','LIN']\n",
    "for i in target:\n",
    "    mask += get_region_mask(i)\n",
    "X = {'terminal':[],'color':[]}\n",
    "for neuron,neuron_color in zip(cortex_neuron_name,neuron_color_mapping):\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            X['terminal'].append(terminal.copy())\n",
    "            if neuron_terminals[neuron]['mirror_mark']:\n",
    "                X['terminal'][-1][2] = 11400-X['terminal'][-1][2]\n",
    "            X['color'].append(neuron_color.tolist())\n",
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "draw_points(neuronvis,X['terminal'],X['color'])\n",
    "for i in target:\n",
    "    neuronvis.addRegion(i)\n",
    "neuronvis.render.setPointSize(2)\n",
    "for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "    set_view(neuronvis,i)\n",
    "    neuronvis.render.savepng(system_path+'reticular_'+i+'.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## central gray"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mask = 0.0\n",
    "target = ['PAG','PCG']\n",
    "for i in target:\n",
    "    mask += get_region_mask(i)\n",
    "X = {'terminal':[],'color':[]}\n",
    "for neuron,neuron_color in zip(cortex_neuron_name,neuron_color_mapping):\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            X['terminal'].append(terminal.copy())\n",
    "            if neuron_terminals[neuron]['mirror_mark']:\n",
    "                X['terminal'][-1][2] = 11400-X['terminal'][-1][2]\n",
    "            X['color'].append(neuron_color.tolist())\n",
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "draw_points(neuronvis,X['terminal'],X['color'])\n",
    "for i in target:\n",
    "    neuronvis.addRegion(i)\n",
    "neuronvis.render.setPointSize(2)\n",
    "for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "    set_view(neuronvis,i)\n",
    "    neuronvis.render.savepng(system_path+'central_'+i+'.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## modulator system"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mask = 0.0\n",
    "target = ['SNc','VTA','LC','RAmb','CS','RPO','MY-sat']\n",
    "for i in target:\n",
    "    mask += get_region_mask(i)\n",
    "X = {'terminal':[],'color':[]}\n",
    "for neuron,neuron_color in zip(cortex_neuron_name,neuron_color_mapping):\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            X['terminal'].append(terminal.copy())\n",
    "            if neuron_terminals[neuron]['mirror_mark']:\n",
    "                X['terminal'][-1][2] = 11400-X['terminal'][-1][2]\n",
    "            X['color'].append(neuron_color.tolist())\n",
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "draw_points(neuronvis,X['terminal'],X['color'])\n",
    "for i in target:\n",
    "    neuronvis.addRegion(i)\n",
    "neuronvis.render.setPointSize(2)\n",
    "for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "    set_view(neuronvis,i)\n",
    "    neuronvis.render.savepng(system_path+'modulator_'+i+'.png')\n",
    "neuronvis.render.closeWindow()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## motor nuclei"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mask = 0.0\n",
    "target = ['INC','ND','MA3','EW','III','Su3','IV','DTN','PDTg','SG','VI','PRP','DMX','XII','RN','PPN','V','SUT','P5','VII','AMB','PGRNl']\n",
    "for i in target:\n",
    "    mask += get_region_mask(i)\n",
    "X = {'terminal':[],'color':[]}\n",
    "for neuron,neuron_color in zip(cortex_neuron_name,neuron_color_mapping):\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            X['terminal'].append(terminal.copy())\n",
    "            if neuron_terminals[neuron]['mirror_mark']:\n",
    "                X['terminal'][-1][2] = 11400-X['terminal'][-1][2]\n",
    "            X['color'].append(neuron_color.tolist())\n",
    "neuronvis = nv.neuronVis(size=(1920,1080))\n",
    "neuronvis.render.setBackgroundColor((1.0,1.0,1.0,1.0))\n",
    "neuronvis.clear(root=True)\n",
    "draw_points(neuronvis,np.array(X['terminal']).tolist(),np.array(X['color']).tolist())\n",
    "for i in target:\n",
    "    neuronvis.addRegion(i)\n",
    "neuronvis.render.setPointSize(2)\n",
    "for i in ['anterior','posterior','left','right','dorsal','ventral']:\n",
    "    set_view(neuronvis,i)\n",
    "    neuronvis.render.savepng(system_path+'motor_'+i+'.png')\n",
    "neuronvis.render.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# gene"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gene_path = data_path+'gene/'\n",
    "create_dir(gene_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = 200\n",
    "s = 125\n",
    "transition_color = np.array([[255,255,255],[161,238,204],[255,242,193],[246,49,141]])/255.0\n",
    "cmap1 = mcolors.LinearSegmentedColormap.from_list('cmap1', transition_color)\n",
    "kernel = generate_gaussian_kernel(r,s)\n",
    "for kb in brainstem_region:#[brainstem_region.index('DCO'):]:\n",
    "    info = {}\n",
    "    for i in neuron_to_brainstem_information:\n",
    "        d = neuron_to_brainstem_information[i]['soma_depth']\n",
    "        p = neuron_to_brainstem_information[i]['soma_flatten_position']\n",
    "        info[i] = [d,p]\n",
    "        info[i].append(neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'][brainstem_region.index(kb)])\n",
    "    tmp = _flatmap[...,0]*0.0\n",
    "    soma_list = [[],[]]\n",
    "    for i in info:\n",
    "        d,p = info[i][:2]\n",
    "        e = info[i][2]\n",
    "        fill_kernel(tmp,np.array(p[::-1])*2,e/neuron_to_brainstem_information[i]['relative_density'],r,kernel)\n",
    "        soma_list[0].append(np.array(p[::-1])*2)\n",
    "        soma_list[1].append(e)\n",
    "    soma_list[1] = np.array(soma_list[1])**0.5\n",
    "    soma_list[1] /= np.max(soma_list[1])\n",
    "    #result = plot_transition_color(transition_color,tmp)\n",
    "    result = np.where(_flatmap[...,0]==-1,0,tmp)\n",
    "    for k,l in zip(soma_list[0],soma_list[1]):\n",
    "        try:\n",
    "            cv.circle(result,[int(np.round(k[1])), int(np.round(k[0]))],int(np.round(l*10))+1,np.array([0.5,0.5,0.5]),-1)\n",
    "        except:\n",
    "            pass\n",
    "    _max = np.max(result)\n",
    "    _result = result/(1 if _max==0 else _max)\n",
    "    fig,ax = plt.subplots(figsize=(5,5))\n",
    "    ax.imshow(_result,cmap=cmap1,vmin=0,vmax=1,zorder=0)\n",
    "    ax.imshow(flatmap_mask,zorder=1)\n",
    "    ax.imshow(flatmap,zorder=2)\n",
    "    ax.set_xlim(flatten_map_r[0],flatten_map_r[1])\n",
    "    ax.set_ylim(flatten_map_r[2],flatten_map_r[3])\n",
    "    ax.set_title('Soma position coding',color='k',fontsize=fontsize_title)\n",
    "    ax.set_axis_off()\n",
    "    _max = np.max([info[i][-1] for i in info])\n",
    "    plt.title(kb+' ('+str(_max)+')',fontsize=fontsize_title)\n",
    "    np.save(gene_path+kb+'.npy',{'info':info,'pdf':result})\n",
    "    plt.savefig(gene_path+kb+'_origion.pdf',format='pdf',bbox_inches='tight')\n",
    "    plt.savefig(gene_path+kb+'_origion.png',format='png',bbox_inches='tight')\n",
    "    plt.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gg = np.max([neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'] for i in neuron_to_brainstem_information],axis=-1)\n",
    "r = 200\n",
    "s = 125\n",
    "gg = np.max([neuron_to_brainstem_information[i]['axonal_projection_terminal_combined'] for i in neuron_to_brainstem_information],axis=-1)\n",
    "transition_color = np.array([[255,255,255],[161,238,204],[255,242,193],[246,49,141]])/255.0\n",
    "cmap1 = mcolors.LinearSegmentedColormap.from_list('cmap1', transition_color)\n",
    "kernel = generate_gaussian_kernel(r,s)\n",
    "for kb in brainstem_region[brainstem_region.index('CS'):]:#[brainstem_region.index('DCO'):]:\n",
    "    result = np.load(gene_path+kb+'.npy',allow_pickle=True).item()['pdf']\n",
    "    _max = np.max(result)\n",
    "    _result = result/(1 if _max==0 else _max)\n",
    "    fig,ax = plt.subplots(figsize=(5,5))\n",
    "    ax.imshow(_result,cmap=cmap1,vmin=0,vmax=1,zorder=0)\n",
    "    ax.imshow(flatmap_mask,zorder=1)\n",
    "    ax.imshow(flatmap,zorder=2)\n",
    "    ax.set_xlim(flatten_map_r[0],flatten_map_r[1])\n",
    "    ax.set_ylim(flatten_map_r[2],flatten_map_r[3])\n",
    "    ax.set_title('Soma position coding',color='k',fontsize=fontsize_title)\n",
    "    ax.set_axis_off()\n",
    "    _max = gg[brainstem_region.index(kb)]\n",
    "    plt.title(kb+' ('+str(_max)+')',fontsize=fontsize_title)\n",
    "    plt.savefig(gene_path+kb+'_origion.pdf',format='pdf',bbox_inches='tight')\n",
    "    plt.savefig(gene_path+kb+'_origion.png',format='png',bbox_inches='tight')\n",
    "    plt.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## gene data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sqlite3\n",
    "import glob\n",
    "import csv\n",
    "#! sudo mount -t cifs -o username=bap,password=bap12161216 //10.10.31.31,share=share/stereoMouseData/stereo2023/gene_3D"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def gene_file_to_array(file,ID_start,database,cursor,threshold=10000):\n",
    "    id = ID_start\n",
    "    f = open(file)\n",
    "    f.readline()\n",
    "    tmp = csv.reader(f)\n",
    "    co = 0\n",
    "    for i in tmp:\n",
    "        j = i[0].split('\\t')\n",
    "        if j[5]!='0':\n",
    "            cursor.execute('INSERT INTO gene(ID,name,x,y,z,region) VALUES (?,?,?,?,?,?)',[id,j[0],float(j[-4]),float(j[-3]),float(j[-2]),int(j[-1])])\n",
    "            id += 1\n",
    "            co += 1\n",
    "            if co>threshold:\n",
    "                database.commit()\n",
    "                co = 0\n",
    "    if co>0:\n",
    "        database.commit()\n",
    "    f.close()\n",
    "    return id\n",
    "gene_database = sqlite3.Connection(gene_path+'gene_database.db')\n",
    "cursor = gene_database.cursor()\n",
    "cursor.execute('CREATE TABLE IF NOT EXISTS gene(ID INTEGER PRIMARY KEY, name TEXT, x FLOAT, y FLOAT, z FLOAT, region INTEGER)')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ID = 0\n",
    "for file in tool_bar(glob.glob('/run/user/1000/gvfs/smb-share:server=10.10.31.31,share=share/stereoMouseData/stereo2023/gene_3D/*')):\n",
    "    ID = gene_file_to_array(file,ID,gene_database,cursor)\n",
    "cursor.close()\n",
    "gene_database.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = cursor.execute('SELECT * FROM gene where region in (853)')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in a:\n",
    "    print(i)\n",
    "    break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "c = 0\n",
    "for i in a:\n",
    "    print(i)\n",
    "    c += 1\n",
    "    if c>10:\n",
    "        break"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# dendrite"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dendrite_path = data_path+'dendrite/'\n",
    "create_dir(dendrite_path)\n",
    "if not os.path.exists(dendrite_path+'dendrite_info.npy'):\n",
    "    dendrite_info = {}\n",
    "    for neuron in tool_bar(cortex_neuron_name,desc='generating neuron information...',leave=False):\n",
    "        control_console_output(0)\n",
    "        neuron_tree = iondata.getNeuronTreeByID(neuron[:-7],neuron[-7:])\n",
    "        dendrite_info[neuron] = {'point':[],'flatten_point':[]} #xyz,xyd\n",
    "        for terminal in neuron_tree.terminals:\n",
    "            if terminal.type!=3:\n",
    "                continue\n",
    "            dendrite_info[neuron]['point'].append(terminal.xyz)\n",
    "        if len(dendrite_info[neuron]['point'])!=0:\n",
    "            tmp = []\n",
    "            for i in dendrite_info[neuron]['point']:\n",
    "                tmp.append(get_mirrored_point(i))\n",
    "            for i in compute_flatten_position_and_depth(np.array(tmp)):\n",
    "                xyd = [0,0,i[0]]\n",
    "                if len(i[3])!=0:\n",
    "                    xyd[0] = i[3][0]\n",
    "                    xyd[1] = i[3][1]\n",
    "                dendrite_info[neuron]['flatten_point'].append(xyd)\n",
    "        control_console_output(1)\n",
    "    np.save(dendrite_path+'dendrite_info.npy',dendrite_info)\n",
    "else:\n",
    "    dendrite_info = np.load(dendrite_path+'dendrite_info.npy',allow_pickle=True).item()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## bilayer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "parameter=[0,1200,1,15]\n",
    "X = []\n",
    "error = 0\n",
    "no_error = 0\n",
    "for i in dendrite_info:\n",
    "    for j in dendrite_info[i]['flatten_point']:\n",
    "        if j[2]!=0:\n",
    "            no_error += 1\n",
    "            X.append(j[2]*20)\n",
    "        else:\n",
    "            error += 1\n",
    "print('Total data: \\n','error: ',error,'\\n','no_error: ',no_error,'\\n','error_rate: ',np.round(error/(error+no_error)*100,1),'%')\n",
    "distribution_discrete,distribution_continuous = estimate_density_function(X,parameter)\n",
    "plt.figure(figsize=(4,8))\n",
    "plt.barh(range(parameter[1]),distribution_discrete,color=color_pool[1])\n",
    "plt.plot(distribution_continuous,range(parameter[1]),color=color_pool[0])\n",
    "plt.legend()\n",
    "plt.ylim(parameter[1],parameter[0])\n",
    "plt.xlabel('Probability',fontsize=fontsize_label)\n",
    "plt.ylabel('Depth (\\u00B5m)',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.title('Dendrite terminal depth distribution',fontsize=fontsize_title)\n",
    "plt.savefig(dendrite_path+'dendrite_feature_bilayers_proof.pdf',format='pdf')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.mixture import GaussianMixture\n",
    "gmm = GaussianMixture(n_components=2)\n",
    "gmm.fit(np.array(X).reshape(-1,1))\n",
    "print(\"均值:\\n\", gmm.means_)\n",
    "print(\"协方差矩阵:\\n\", gmm.covariances_)\n",
    "print(\"混合系数:\\n\", gmm.weights_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize=(4,8))\n",
    "x = np.array([i for i in range(1200)])\n",
    "y = np.exp(-(x-gmm.means_)**2/(2*gmm.covariances_[...,0]))/(gmm.covariances_[...,0]**0.5*(2*np.pi)**0.5)\n",
    "plt.plot(y[1]*gmm.weights_[1],x,color=color_pool[0],label='tuft dendrite',linewidth=2,linestyle='dashed')\n",
    "plt.plot(y[0]*gmm.weights_[0],x,color=color_pool[3],label='basal dendrite',linewidth=2,linestyle='dashed')\n",
    "plt.legend()\n",
    "plt.ylim(parameter[1],parameter[0])\n",
    "plt.xlabel('Probability',fontsize=fontsize_label)\n",
    "plt.ylabel('Depth (\\u00B5m)',fontsize=fontsize_label,rotation=-90).set_verticalalignment('top')\n",
    "plt.title('Dendrite terminal depth distribution',fontsize=fontsize_title)\n",
    "plt.savefig(dendrite_path+'dendrite_feature_bilayers_proof2.pdf',format='pdf')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_m = np.sum(y*gmm.weights_.reshape(-1,1),axis=0)\n",
    "plt.plot(distribution_continuous)\n",
    "plt.plot(y_m)\n",
    "plt.show()\n",
    "print(np.sum(distribution_continuous)-np.sum(np.min((y_m,distribution_continuous),axis=0))*100)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## columnar"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "threshold = 100\n",
    "error = 0\n",
    "no_error = 0\n",
    "column_info = {}\n",
    "for i in dendrite_info:\n",
    "    X = []\n",
    "    Y = []\n",
    "    for j in dendrite_info[i]['flatten_point']:\n",
    "        if j[2]!=0:\n",
    "            if j[2]*20<=threshold:\n",
    "                X.append(j[:2])\n",
    "            else:\n",
    "                Y.append(j[:2])\n",
    "            no_error += 1\n",
    "        else:\n",
    "            error += 1\n",
    "    column_info[i] = [np.linalg.norm(np.std(X,axis=0)) if len(X)!=0 else 0,np.linalg.norm(np.std(Y,axis=0)) if len(Y)!=0 else 0]\n",
    "tmp = np.array(list(column_info.values()))\n",
    "a = np.where(tmp[:,0]==0,False,True)\n",
    "b = np.where(tmp[:,1]==0,False,True)\n",
    "flatten_map_scale = 10\n",
    "plt.scatter(tmp[:,0][a*b]*flatten_map_scale,tmp[:,1][a*b]*flatten_map_scale,s=0.5,color=color_pool[1])\n",
    "plt.scatter(tmp[:,0][~a*b]*flatten_map_scale,tmp[:,1][~a*b]*flatten_map_scale,s=0.5,color=color_pool[3])\n",
    "from scipy import stats\n",
    "r = stats.pearsonr(tmp[:,0][a*b]*flatten_map_scale,tmp[:,1][a*b]*flatten_map_scale)\n",
    "coefficients = np.polyfit(tmp[:,0][a*b]*flatten_map_scale,tmp[:,1][a*b]*flatten_map_scale,1)\n",
    "print('have dendrite: neurons: ',len(tmp),'ratio of all: ',np.round(len(tmp)/len(cortex_neuron_name)*100,1),'%\\n',\n",
    "'no pia dendritic neurons: ',len(tmp)-np.sum(a*b),'ratio of all: ',np.round((len(tmp)-np.sum(a*b))/len(tmp)*100,1),'%\\n',\n",
    "'pearson_r: ',r,'\\n',\n",
    "'coefficients: ',coefficients)\n",
    "plt.axline([0,coefficients[1]],slope=coefficients[0],color=color_pool[0],linestyle='dashed')\n",
    "plt.axline([0,0],slope=1,color='#555555',linestyle='dashed')\n",
    "\n",
    "from matplotlib.patches import Ellipse\n",
    "mean = [np.mean(tmp[:,0][a*b]*flatten_map_scale), np.mean(tmp[:,1][a*b]*flatten_map_scale)]\n",
    "cov = np.cov(tmp[:,0][a*b]*flatten_map_scale,tmp[:,1][a*b]*flatten_map_scale)\n",
    "eigenvalues, eigenvectors = np.linalg.eig(cov)\n",
    "std_devs = np.sqrt(eigenvalues)\n",
    "ellipse = Ellipse(mean, std_devs[0]*2,std_devs[1]*2,angle=np.rad2deg(np.arccos(eigenvectors[0,0])),edgecolor='black',linewidth=1,linestyle='dashed',facecolor='none',alpha=1)\n",
    "plt.gca().add_patch(ellipse)\n",
    "print('average column for tuft and basal dendrite: ',mean)\n",
    "\n",
    "plt.xlabel('Tuft dendritic terminal std. (\\u00B5m)',fontsize=15)\n",
    "plt.ylabel('Basal dendritic terminal std. (\\u00B5m)',fontsize=15,rotation=-90).set_verticalalignment('top')\n",
    "plt.title('Dendritic range distribution',fontsize=17)\n",
    "plt.xlim(-5,60*flatten_map_scale)\n",
    "plt.ylim(-5,60*flatten_map_scale)\n",
    "plt.gca().set_aspect('equal', adjustable='box')\n",
    "plt.savefig(dendrite_path+'dendrite_feature_column_proof.pdf',format='pdf')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.decomposition import PCA\n",
    "threshold = 100\n",
    "error = 0\n",
    "no_error = 0\n",
    "shape_info = {}\n",
    "for i in dendrite_info:\n",
    "    X = []\n",
    "    Y = []\n",
    "    for j in dendrite_info[i]['flatten_point']:\n",
    "        if j[2]!=0:\n",
    "            if j[2]*20<=threshold:\n",
    "                X.append(j)\n",
    "            else:\n",
    "                Y.append(j)\n",
    "            no_error += 1\n",
    "        else:\n",
    "            error += 1\n",
    "    if len(X)<=2:\n",
    "        a = [0,0,0]\n",
    "    else:\n",
    "        pca = PCA(n_components=3)\n",
    "        pca.fit(X)\n",
    "        std_deviation = np.sqrt(pca.explained_variance_)\n",
    "        a = std_deviation\n",
    "    if len(Y)<=2:\n",
    "        b = [0,0,0]\n",
    "    else:\n",
    "        pca = PCA(n_components=3)\n",
    "        pca.fit(Y)\n",
    "        std_deviation = np.sqrt(pca.explained_variance_)\n",
    "        b = std_deviation\n",
    "    shape_info[i] = [a,b]\n",
    "X = []\n",
    "Y = []\n",
    "target_vector = np.array([1.0,1.0,1.0])\n",
    "target_vector /= np.linalg.norm(target_vector)\n",
    "for i in shape_info:\n",
    "    a = np.array(shape_info[i][0])\n",
    "    if np.sum(a**2)!=0:\n",
    "        a /= np.sum(a)\n",
    "        X.append(np.std(a))\n",
    "    b = np.array(shape_info[i][1])\n",
    "    if np.sum(b**2)!=0:\n",
    "        b /= np.sum(b)\n",
    "        Y.append(np.std(b))\n",
    "a,b,c = [np.std([0.33,0.33,0.33]),np.std([0.5,0.5,0]),np.std([1,0,0])]\n",
    "parameter=[a,1001,(c-a)/1000,(c-a)*15/1300]\n",
    "plt.figure(figsize=(15,5))\n",
    "distribution_discrete,distribution_continuous = estimate_density_function(X,parameter)\n",
    "plt.plot(distribution_continuous,color=color_pool[0],label='tuft dendrite')\n",
    "plt.bar(range(parameter[1]),distribution_discrete,color=color_pool[0])\n",
    "distribution_discrete,distribution_continuous = estimate_density_function(Y,parameter)\n",
    "plt.plot(distribution_continuous,color=color_pool[3],label='basal dendrite')\n",
    "plt.bar(range(parameter[1]),distribution_discrete,color=color_pool[3])\n",
    "plt.legend()\n",
    "#plt.xlim(a,c)\n",
    "plt.xlabel('Spherial <---> Plan <---> Line index',fontsize=15)\n",
    "plt.ylabel('Probability',fontsize=15,rotation=-90).set_verticalalignment('top')\n",
    "plt.title('Dendrite shape distribution',fontsize=17)\n",
    "plt.savefig(dendrite_path+'dendrite_feature_shape_proof.pdf',format='pdf')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# garbage"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## pons cerebellar system"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mask = get_region_mask('PG')+get_region_mask('TRN')\n",
    "X = {}\n",
    "for id,neuron in enumerate(neuron_to_brainstem_information):\n",
    "    region = neuron_to_brainstem_information[neuron]['cortex_region']\n",
    "    if region not in X:\n",
    "        X[region] = {'terminal':[]}\n",
    "    for terminal in neuron_terminals[neuron]['terminals']:\n",
    "        if if_inside_mask(mask,np.array(terminal)/10):\n",
    "            X[region]['terminal'].append(terminal.copy())\n",
    "            if neuron_terminals[neuron]['mirror_mark']:\n",
    "                X[region]['terminal'][-1][2] = 11400-X[region]['terminal'][-1][2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "Y = {}\n",
    "for i in X:\n",
    "    module = cortical_module_transform[i]\n",
    "    if module not in Y:\n",
    "        Y[module] = 0.0\n",
    "    Y[module] += len(X[i]['terminal'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.bar(range(len(Y)),Y.values())\n",
    "_ = plt.xticks(range(len(Y)),Y.keys())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "_max = float(np.sum(list(Y.values())))\n",
    "for i in Y:\n",
    "    print(i,Y[i],np.round(Y[i]/_max*100,0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "28.6+29.8+4.3+26.2+0.5+8.5+2.2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "_max"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.2"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
